JasPer: jas_malloc.h Source File

JasPer 3.0.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
86extern "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
133} jas_allocator_t;
134
143 typedef struct {
144
145 /* The base class. */
146 jas_allocator_t base;
147
148} jas_std_allocator_t;
149
150#if defined(JAS_FOR_INTERNAL_USE_ONLY)
151/*
152The allocator wrapper type.
153This type is an allocator that adds memory usage tracking to another
154allocator.
155The allocator wrapper does not directly perform memory allocation itself.
156Instead, it delegate to another allocator.
157*/
158typedef 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)
185extern jas_allocator_t *jas_allocator;
186extern jas_std_allocator_t jas_std_allocator;
187extern jas_basic_allocator_t jas_basic_allocator;
188#endif
189
190/******************************************************************************\
191* Functions.
192\******************************************************************************/
193
203JAS_EXPORT
204void *jas_malloc(size_t size);
205
213JAS_EXPORT
214void jas_free(void *ptr);
215
224JAS_EXPORT
225void *jas_realloc(void *ptr, size_t size);
226
235JAS_EXPORT
236void *jas_calloc(size_t num_elements, size_t element_size);
237
242JAS_EXPORT
243void *jas_alloc2(size_t num_elements, size_t element_size);
244
249JAS_EXPORT
250void *jas_alloc3(size_t num_arrays, size_t array_size, size_t element_size);
251
256JAS_EXPORT
257void *jas_realloc2(void *ptr, size_t num_elements, size_t element_size);
258
280JAS_EXPORT
281void jas_set_max_mem_usage(size_t max_mem);
282
295JAS_EXPORT
296size_t jas_get_mem_usage(void);
297
311JAS_EXPORT
312void jas_std_allocator_init(jas_std_allocator_t *allocator);
313
323JAS_EXPORT
324void 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. */
329void jas_set_allocator(jas_allocator_t* allocator);
330
331/* This function is for internal library use only. */
332void jas_basic_allocator_init(jas_basic_allocator_t *allocator,
333 jas_allocator_t *delegate, size_t max_mem);
334
335#endif
336
352JAS_EXPORT
353size_t jas_get_total_mem_size(void);
354
359#ifdef __cplusplus
360}
361#endif
362
363#endif
jas_alloc2
JAS_EXPORT void * jas_alloc2(size_t num_elements, size_t element_size)
Allocate array (with overflow checking).
Definition: jas_malloc.c:212
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_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_set_max_mem_usage
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_calloc
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_realloc
JAS_EXPORT void * jas_realloc(void *ptr, size_t size)
Resize a block of allocated memory.
Definition: jas_malloc.c:151
jas_alloc3
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_realloc2
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
jas_allocator_t
struct jas_allocator_s jas_allocator_t
A memory allocator.
jas_malloc
JAS_EXPORT void * jas_malloc(size_t size)
Allocate memory.
Definition: jas_malloc.c:136
jas_get_total_mem_size
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_get_mem_usage
JAS_EXPORT size_t jas_get_mem_usage(void)
Get the current memory usage from the allocator wrapper.
Definition: jas_malloc.c:370
jas_free
JAS_EXPORT void jas_free(void *ptr)
Free memory.
Definition: jas_malloc.c:186
jas_thread.h
Threads.
jas_types.h
Primitive Types.
jas_allocator_s
A memory allocator.
Definition: jas_malloc.h:101
jas_allocator_s::cleanup
void(* cleanup)(struct jas_allocator_s *allocator)
Definition: jas_malloc.h:109
jas_allocator_s::free
void(* free)(struct jas_allocator_s *allocator, void *pointer)
Definition: jas_malloc.h:121
jas_allocator_s::reserved
void reserved(void)
jas_std_allocator_t
The standard library allocator (i.e., a wrapper for malloc and friends).
Definition: jas_malloc.h:143