JasPer  4.2.4
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_EXPORT void * jas_malloc(size_t size)
Allocate memory.
Definition: jas_malloc.c:136
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_EXPORT 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_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_EXPORT 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_EXPORT void * jas_alloc2(size_t num_elements, size_t element_size)
Allocate array (with overflow checking).
Definition: jas_malloc.c:212
JAS_EXPORT 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
struct jas_allocator_s jas_allocator_t
A memory allocator.
JAS_EXPORT size_t jas_get_total_mem_size(void)
Get the total amount of memory available on the system.
Definition: jas_malloc.c:627
JAS_EXPORT 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_EXPORT size_t jas_get_mem_usage(void)
Get the current memory usage from the allocator wrapper.
Definition: jas_malloc.c:370
JAS_EXPORT void * jas_realloc(void *ptr, size_t size)
Resize a block of allocated memory.
Definition: jas_malloc.c:151
JAS_EXPORT void jas_free(void *ptr)
Free memory.
Definition: jas_malloc.c:186
Threads.
Primitive Types.
A memory allocator.
Definition: jas_malloc.h:101
void(* cleanup)(struct jas_allocator_s *allocator)
Definition: jas_malloc.h:109
void(* free)(struct jas_allocator_s *allocator, void *pointer)
Definition: jas_malloc.h:121
void reserved(void)
The standard library allocator (i.e., a wrapper for malloc and friends).
Definition: jas_malloc.h:143