Update the changelog
[opencv] / cxcore / src / cxalloc.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include "_cxcore.h"
43
44 // default <malloc>
45 static void*
46 icvDefaultAlloc( size_t size, void* )
47 {
48     char *ptr, *ptr0 = (char*)malloc(
49         (size_t)(size + CV_MALLOC_ALIGN*((size >= 4096) + 1) + sizeof(char*)));
50
51     if( !ptr0 )
52         return 0;
53
54     // align the pointer
55     ptr = (char*)cvAlignPtr(ptr0 + sizeof(char*) + 1, CV_MALLOC_ALIGN);
56     *(char**)(ptr - sizeof(char*)) = ptr0;
57
58     return ptr;
59 }
60
61
62 // default <free>
63 static int
64 icvDefaultFree( void* ptr, void* )
65 {
66     // Pointer must be aligned by CV_MALLOC_ALIGN
67     if( ((size_t)ptr & (CV_MALLOC_ALIGN-1)) != 0 )
68         return CV_BADARG_ERR;
69     free( *((char**)ptr - 1) );
70
71     return CV_OK;
72 }
73
74
75 // pointers to allocation functions, initially set to default
76 static CvAllocFunc p_cvAlloc = icvDefaultAlloc;
77 static CvFreeFunc p_cvFree = icvDefaultFree;
78 static void* p_cvAllocUserData = 0;
79
80 CV_IMPL void cvSetMemoryManager( CvAllocFunc alloc_func, CvFreeFunc free_func, void* userdata )
81 {
82     CV_FUNCNAME( "cvSetMemoryManager" );
83
84     __BEGIN__;
85     
86     if( (alloc_func == 0) ^ (free_func == 0) )
87         CV_ERROR( CV_StsNullPtr, "Either both pointers should be NULL or none of them");
88
89     p_cvAlloc = alloc_func ? alloc_func : icvDefaultAlloc;
90     p_cvFree = free_func ? free_func : icvDefaultFree;
91     p_cvAllocUserData = userdata;
92
93     __END__;
94 }
95
96
97 CV_IMPL  void*  cvAlloc( size_t size )
98 {
99     void* ptr = 0;
100     
101     CV_FUNCNAME( "cvAlloc" );
102
103     __BEGIN__;
104
105     if( (size_t)size > CV_MAX_ALLOC_SIZE )
106         CV_ERROR( CV_StsOutOfRange,
107                   "Negative or too large argument of cvAlloc function" );
108
109     ptr = p_cvAlloc( size, p_cvAllocUserData );
110     if( !ptr )
111         CV_ERROR( CV_StsNoMem, "Out of memory" );
112
113     __END__;
114
115     return ptr;
116 }
117
118
119 CV_IMPL  void  cvFree_( void* ptr )
120 {
121     CV_FUNCNAME( "cvFree_" );
122
123     __BEGIN__;
124
125     if( ptr )
126     {
127         CVStatus status = p_cvFree( ptr, p_cvAllocUserData );
128         if( status < 0 )
129             CV_ERROR( status, "Deallocation error" );
130     }
131
132     __END__;
133 }
134
135 /* End of file. */