JasPer  3.0.5
jas_malloc.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3  * British Columbia.
4  * Copyright (c) 2001-2002 Michael David Adams.
5  * All rights reserved.
6  */
7 
8 /* __START_OF_JASPER_LICENSE__
9  *
10  * JasPer License Version 2.0
11  *
12  * Copyright (c) 2001-2006 Michael David Adams
13  * Copyright (c) 1999-2000 Image Power, Inc.
14  * Copyright (c) 1999-2000 The University of British Columbia
15  *
16  * All rights reserved.
17  *
18  * Permission is hereby granted, free of charge, to any person (the
19  * "User") obtaining a copy of this software and associated documentation
20  * files (the "Software"), to deal in the Software without restriction,
21  * including without limitation the rights to use, copy, modify, merge,
22  * publish, distribute, and/or sell copies of the Software, and to permit
23  * persons to whom the Software is furnished to do so, subject to the
24  * following conditions:
25  *
26  * 1. The above copyright notices and this permission notice (which
27  * includes the disclaimer below) shall be included in all copies or
28  * substantial portions of the Software.
29  *
30  * 2. The name of a copyright holder shall not be used to endorse or
31  * promote products derived from the Software without specific prior
32  * written permission.
33  *
34  * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35  * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36  * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37  * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39  * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
40  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
45  * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46  * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47  * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48  * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49  * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
50  * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51  * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
52  * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53  * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54  * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55  * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56  * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57  * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58  * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59  * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
60  *
61  * __END_OF_JASPER_LICENSE__
62  */
63 
64 
70 #ifndef JAS_MALLOC_H
71 #define JAS_MALLOC_H
72 
73 /******************************************************************************\
74 * Includes.
75 \******************************************************************************/
76 
77 /* The configuration header file should be included first. */
78 #include <jasper/jas_config.h>
79 
80 #include <jasper/jas_types.h>
81 #include <jasper/jas_thread.h>
82 
83 #include <stdio.h>
84 
85 #ifdef __cplusplus
86 extern "C" {
87 #endif
88 
94 /******************************************************************************\
95 * Types.
96 \******************************************************************************/
97 
101 typedef struct jas_allocator_s {
102 
109  void (*cleanup)(struct jas_allocator_s *allocator);
110 
115  void *(*alloc)(struct jas_allocator_s *allocator, size_t size);
116 
121  void (*free)(struct jas_allocator_s *allocator, void *pointer);
122 
127  void *(*realloc)(struct jas_allocator_s *allocator, void *pointer,
128  size_t new_size);
129 
131  void (*(reserved[4]))(void);
132 
134 
143 typedef struct {
144 
145  /* The base class. */
146  jas_allocator_t base;
147 
149 
150 #if defined(JAS_FOR_INTERNAL_USE_ONLY)
151 /*
152 The allocator wrapper type.
153 This type is an allocator that adds memory usage tracking to another
154 allocator.
155 The allocator wrapper does not directly perform memory allocation itself.
156 Instead, it delegate to another allocator.
157 */
158 typedef struct {
159 
160  /* The base class. */
161  jas_allocator_t base;
162 
163  /* The delegated-to allocator. */
164  jas_allocator_t *delegate;
165 
166  /* The maximum amount of memory that can be used by the allocator. */
167  size_t max_mem;
168 
169  /* The current amount of memory in use by the allocator. */
170  size_t mem;
171 
172 #if defined(JAS_THREADS)
173  /* A mutex for synchronized access to the allocator. */
174  jas_mutex_t mutex;
175 #endif
176 
177 } jas_basic_allocator_t;
178 #endif
179 
180 /******************************************************************************\
181 * Data.
182 \******************************************************************************/
183 
184 #if defined(JAS_FOR_INTERNAL_USE_ONLY)
185 extern jas_allocator_t *jas_allocator;
186 extern jas_std_allocator_t jas_std_allocator;
187 extern jas_basic_allocator_t jas_basic_allocator;
188 #endif
189 
190 /******************************************************************************\
191 * Functions.
192 \******************************************************************************/
193 
203 JAS_EXPORT
204 void *jas_malloc(size_t size);
205 
213 JAS_EXPORT
214 void jas_free(void *ptr);
215 
224 JAS_EXPORT
225 void *jas_realloc(void *ptr, size_t size);
226 
235 JAS_EXPORT
236 void *jas_calloc(size_t num_elements, size_t element_size);
237 
242 JAS_EXPORT
243 void *jas_alloc2(size_t num_elements, size_t element_size);
244 
249 JAS_EXPORT
250 void *jas_alloc3(size_t num_arrays, size_t array_size, size_t element_size);
251 
256 JAS_EXPORT
257 void *jas_realloc2(void *ptr, size_t num_elements, size_t element_size);
258 
280 JAS_EXPORT
281 void jas_set_max_mem_usage(size_t max_mem);
282 
295 JAS_EXPORT
296 size_t jas_get_mem_usage(void);
297 
311 JAS_EXPORT
313 
323 JAS_EXPORT
324 void jas_allocator_cleanup(jas_allocator_t *allocator);
325 
326 #if defined(JAS_FOR_INTERNAL_USE_ONLY)
327 
328 /* This function is for internal library use only. */
329 void jas_set_allocator(jas_allocator_t* allocator);
330 
331 /* This function is for internal library use only. */
332 void jas_basic_allocator_init(jas_basic_allocator_t *allocator,
333  jas_allocator_t *delegate, size_t max_mem);
334 
335 #endif
336 
352 JAS_EXPORT
353 size_t jas_get_total_mem_size(void);
354 
359 #ifdef __cplusplus
360 }
361 #endif
362 
363 #endif
jas_allocator_s::alloc
void *(* alloc)(struct jas_allocator_s *allocator, size_t size)
Definition: jas_malloc.h:115
jas_logwarnf
JAS_EXPORT int jas_logwarnf(const char *fmt,...)
Generate a warning log message.
Definition: jas_debug.c:131
jas_malloc.h
JasPer Memory Allocator.
jas_alloc3
void * jas_alloc3(size_t num_arrays, size_t array_size, size_t element_size)
Allocate array of arrays (with overflow checking).
Definition: jas_malloc.c:221
jas_std_allocator_init
JAS_EXPORT void jas_std_allocator_init(jas_std_allocator_t *allocator)
Initialize a memory allocator that uses malloc and related functions for managing memory.
Definition: jas_malloc.c:262
jas_allocator_cleanup
JAS_EXPORT void jas_allocator_cleanup(jas_allocator_t *allocator)
Clean up an allocator that is no longer needed.
Definition: jas_malloc.c:244
jas_logerrorf
JAS_EXPORT int jas_logerrorf(const char *fmt,...)
Generate an error log message.
Definition: jas_debug.c:119
jas_allocator_s::cleanup
void(* cleanup)(struct jas_allocator_s *allocator)
Definition: jas_malloc.h:109
jas_std_allocator_t
The standard library allocator (i.e., a wrapper for malloc and friends).
Definition: jas_malloc.h:143
jas_realloc2
void * jas_realloc2(void *ptr, size_t num_elements, size_t element_size)
Resize a block of allocated memory (with overflow checking).
Definition: jas_malloc.c:231
jas_get_total_mem_size
size_t jas_get_total_mem_size()
Get the total amount of memory available on the system.
Definition: jas_malloc.c:627
jas_alloc2
void * jas_alloc2(size_t num_elements, size_t element_size)
Allocate array (with overflow checking).
Definition: jas_malloc.c:212
jas_set_max_mem_usage
void jas_set_max_mem_usage(size_t max_mem)
Set the maximum memory usage allowed by the allocator wrapper.
Definition: jas_malloc.c:355
jas_get_mem_usage
size_t jas_get_mem_usage()
Get the current memory usage from the allocator wrapper.
Definition: jas_malloc.c:370
jas_allocator_s::free
void(* free)(struct jas_allocator_s *allocator, void *pointer)
Definition: jas_malloc.h:121
jas_realloc
JAS_EXPORT void * jas_realloc(void *ptr, size_t size)
Resize a block of allocated memory.
Definition: jas_malloc.c:151
jas_allocator_s::reserved
void reserved(void)
jas_calloc
void * jas_calloc(size_t num_elements, size_t element_size)
Allocate a block of memory and initialize the contents to zero.
Definition: jas_malloc.c:198
jas_malloc
JAS_EXPORT void * jas_malloc(size_t size)
Allocate memory.
Definition: jas_malloc.c:136
jas_debug.h
JasPer Debugging-Related Functionality.
jas_free
JAS_EXPORT void jas_free(void *ptr)
Free memory.
Definition: jas_malloc.c:186
jas_types.h
Primitive Types.
jas_thread.h
Threads.
jas_allocator_s::realloc
void *(* realloc)(struct jas_allocator_s *allocator, void *pointer, size_t new_size)
Definition: jas_malloc.h:127
jas_allocator_t
struct jas_allocator_s jas_allocator_t
A memory allocator.
jas_math.h
Math-Related Code.
jas_allocator_s
A memory allocator.
Definition: jas_malloc.h:101