Libmv cleanup: move aligned malloc implementation into own file

It was rather stupid having it in brute region tracker,
now it is in own file in base library.
This commit is contained in:
Sergey Sharybin 2014-04-23 16:02:30 +06:00
parent ad5497b6df
commit c605711c6b
7 changed files with 131 additions and 59 deletions

@ -59,6 +59,7 @@ if(WITH_LIBMV)
list(APPEND SRC
libmv-capi.cc
libmv-util.cc
libmv/base/aligned_malloc.cc
libmv/image/array_nd.cc
libmv/image/convolve.cc
libmv/multiview/conditioning.cc
@ -96,6 +97,7 @@ if(WITH_LIBMV)
third_party/gflags/gflags_reporting.cc
libmv-util.h
libmv/base/aligned_malloc.h
libmv/base/id_generator.h
libmv/base/scoped_ptr.h
libmv/base/vector.h

@ -1,3 +1,20 @@
commit d14049e00dabf8fdf49056779f0a3718fbb39e8f
Author: Sergey Sharybin <sergey.vfx@gmail.com>
Date: Wed Apr 23 15:08:16 2014 +0600
Move aligned malloc implementation into own file
It was rather stupid having it in brute region tracker,
now it is in own file in base library (which was also
added in this commit, before this it consist of header
files only).
Reviewers: keir
Reviewed By: keir
Differential Revision: https://developer.blender.org/D479
commit 0ddf3851bfcb8de43660b119a25a77a25674200d
Author: Sergey Sharybin <sergey.vfx@gmail.com>
Date: Mon Apr 21 14:14:03 2014 +0600
@ -672,12 +689,3 @@ Date: Tue Jun 18 19:24:07 2013 +0600
We could end up with something smarter in the
future, but for now i'm not as much fan of
forcing compiler's flag.
commit 2b90b3915671cb629f7aabed30a88f28450294f8
Author: Sergey Sharybin <sergey.vfx@gmail.com>
Date: Sat Jun 1 16:20:35 2013 +0600
Pass vectors by a reference
Saves couple of time which used to waste on copying objects,
also solves win32 compilation errors caused by alignment.

@ -19,6 +19,7 @@ if env['WITH_BF_LIBMV']:
defs.append('LIBMV_NO_FAST_DETECTOR')
src = env.Glob('*.cc')
src += env.Glob('libmv/base/*.cc')
src += env.Glob('libmv/image/*.cc')
src += env.Glob('libmv/multiview/*.cc')
src += env.Glob('libmv/numeric/*.cc')

@ -1,3 +1,5 @@
libmv/base/aligned_malloc.cc
libmv/base/aligned_malloc.h
libmv/base/id_generator.h
libmv/base/scoped_ptr.h
libmv/base/vector.h

@ -0,0 +1,73 @@
// Copyright (c) 2014 libmv authors.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#include "libmv/base/aligned_malloc.h"
#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
// Needed for memalign on Linux and _aligned_alloc on Windows.
# ifdef FREE_WINDOWS
/* make sure _aligned_malloc is included */
# ifdef __MSVCRT_VERSION__
# undef __MSVCRT_VERSION__
# endif
# define __MSVCRT_VERSION__ 0x0700
# endif // FREE_WINDOWS
# include <malloc.h>
#else
// Apple's malloc is 16-byte aligned, and does not have malloc.h, so include
// stdilb instead.
# include <cstdlib>
#endif
namespace libmv {
void *aligned_malloc(int size, int alignment) {
#ifdef _WIN32
return _aligned_malloc(size, alignment);
#elif __APPLE__
// On Mac OS X, both the heap and the stack are guaranteed 16-byte aligned so
// they work natively with SSE types with no further work.
CHECK_EQ(alignment, 16);
return malloc(size);
#elif defined(__FreeBSD__) || defined(__NetBSD__)
void *result;
if (posix_memalign(&result, alignment, size)) {
// non-zero means allocation error
// either no allocation or bad alignment value
return NULL;
}
return result;
#else // This is for Linux.
return memalign(alignment, size);
#endif
}
void aligned_free(void *ptr) {
#ifdef _WIN32
_aligned_free(ptr);
#else
free(ptr);
#endif
}
} // namespace libmv

@ -0,0 +1,34 @@
// Copyright (c) 2014 libmv authors.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#ifndef LIBMV_BASE_ALIGNED_MALLOC_H_
#define LIBMV_BASE_ALIGNED_MALLOC_H_
namespace libmv {
// Allocate block of size bytes at least aligned to a given value.
void *aligned_malloc(int size, int alignment);
// Free memory allocated by aligned_malloc.
void aligned_free(void *ptr);
} // namespace libmv
#endif // LIBMV_BASE_ALIGNED_MALLOC_H_

@ -21,27 +21,10 @@
#include "libmv/tracking/brute_region_tracker.h"
#ifdef __SSE2__
#include <emmintrin.h>
#endif
#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
// Needed for memalign on Linux and _aligned_alloc on Windows.
#ifdef FREE_WINDOWS
/* make sure _aligned_malloc is included */
#ifdef __MSVCRT_VERSION__
#undef __MSVCRT_VERSION__
#endif
#define __MSVCRT_VERSION__ 0x0700
#endif
#include <malloc.h>
#else
// Apple's malloc is 16-byte aligned, and does not have malloc.h, so include
// stdilb instead.
#include <cstdlib>
# include <emmintrin.h>
#endif
#include "libmv/base/aligned_malloc.h"
#include "libmv/image/image.h"
#include "libmv/image/convolve.h"
#include "libmv/image/correlation.h"
@ -51,37 +34,6 @@
namespace libmv {
namespace {
// TODO(keir): It's stupid that this is needed here. Push this somewhere else.
void *aligned_malloc(int size, int alignment) {
#ifdef _WIN32
return _aligned_malloc(size, alignment);
#elif __APPLE__
// On Mac OS X, both the heap and the stack are guaranteed 16-byte aligned so
// they work natively with SSE types with no further work.
CHECK_EQ(alignment, 16);
return malloc(size);
#elif defined(__FreeBSD__) || defined(__NetBSD__)
void *result;
if (posix_memalign(&result, alignment, size)) {
// non-zero means allocation error
// either no allocation or bad alignment value
return NULL;
}
return result;
#else // This is for Linux.
return memalign(alignment, size);
#endif
}
void aligned_free(void *ptr) {
#ifdef _WIN32
_aligned_free(ptr);
#else
free(ptr);
#endif
}
bool RegionIsInBounds(const FloatImage &image1,
double x, double y,
int half_window_size) {