1 #ifndef CYGONCE_MEMALLOC_DLMALLOCIMPL_HXX
2 #define CYGONCE_MEMALLOC_DLMALLOCIMPL_HXX
4 //==========================================================================
8 // Interface to the port of Doug Lea's malloc implementation
10 //==========================================================================
11 //####ECOSGPLCOPYRIGHTBEGIN####
12 // -------------------------------------------
13 // This file is part of eCos, the Embedded Configurable Operating System.
14 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
16 // eCos is free software; you can redistribute it and/or modify it under
17 // the terms of the GNU General Public License as published by the Free
18 // Software Foundation; either version 2 or (at your option) any later version.
20 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
21 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 // You should have received a copy of the GNU General Public License along
26 // with eCos; if not, write to the Free Software Foundation, Inc.,
27 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
29 // As a special exception, if other files instantiate templates or use macros
30 // or inline functions from this file, or you compile this file and link it
31 // with other works to produce a work based on this file, this file does not
32 // by itself cause the resulting work to be covered by the GNU General Public
33 // License. However the source code for this file must still be made available
34 // in accordance with section (3) of the GNU General Public License.
36 // This exception does not invalidate any other reasons why a work based on
37 // this file might be covered by the GNU General Public License.
39 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
40 // at http://sources.redhat.com/ecos/ecos-license/
41 // -------------------------------------------
42 //####ECOSGPLCOPYRIGHTEND####
43 //==========================================================================
44 //#####DESCRIPTIONBEGIN####
46 // Author(s): jlarmour
49 // Purpose: Define standard interface to Doug Lea's malloc implementation
50 // Description: Doug Lea's malloc has been ported to eCos. This file provides
51 // the interface between the implementation and the standard
52 // memory allocator interface required by eCos
53 // Usage: #include <cyg/memalloc/dlmalloc.hxx>
56 //####DESCRIPTIONEND####
58 //==========================================================================
62 #include <pkgconf/memalloc.h>
66 #include <stddef.h> // size_t, ptrdiff_t
67 #include <cyg/infra/cyg_type.h> // types
69 #include <cyg/memalloc/common.hxx> // Common memory allocator infra
71 // As a special case, override CYGIMP_MEMALLOC_ALLOCATOR_DLMALLOC_SAFE_MULTIPLE
72 // if the malloc config says so
73 #ifdef CYGIMP_MEMALLOC_MALLOC_DLMALLOC
74 // forward declaration to prevent header dependency problems
75 class Cyg_Mempool_dlmalloc;
76 # include <pkgconf/heaps.hxx>
77 # if (CYGMEM_HEAP_COUNT > 1) && \
78 !defined(CYGIMP_MEMALLOC_ALLOCATOR_DLMALLOC_SAFE_MULTIPLE)
79 # define CYGIMP_MEMALLOC_ALLOCATOR_DLMALLOC_SAFE_MULTIPLE 1
85 // number of bins - but changing this alone will not change the number of
87 #define CYGPRI_MEMALLOC_ALLOCATOR_DLMALLOC_NAV 128
92 class Cyg_Mempool_dlmalloc_Implementation
95 /* cyg_dlmalloc_size_t is the word-size used for internal bookkeeping
96 of chunk sizes. On a 64-bit machine, you can reduce malloc
97 overhead, especially for very small chunks, by defining
98 cyg_dlmalloc_size_t to be a 32-bit type at the expense of not
99 being able to handle requests greater than 2^31. This limitation is
100 hardly ever a concern; you are encouraged to set this. However, the
101 default version is the same as size_t. */
103 typedef size_t Cyg_dlmalloc_size_t;
105 typedef struct malloc_chunk
107 Cyg_dlmalloc_size_t prev_size; /* Size of previous chunk (if free). */
108 Cyg_dlmalloc_size_t size; /* Size in bytes, including overhead. */
109 struct malloc_chunk* fd; /* double links -- used only if free. */
110 struct malloc_chunk* bk;
114 /* The first value returned from sbrk */
115 cyg_uint8 *arenabase;
117 /* The total memory in the pool */
120 #ifdef CYGIMP_MEMALLOC_ALLOCATOR_DLMALLOC_SAFE_MULTIPLE
121 struct Cyg_Mempool_dlmalloc_Implementation::malloc_chunk *
122 av_[ CYGPRI_MEMALLOC_ALLOCATOR_DLMALLOC_NAV * 2 + 2 ];
125 #ifdef CYGDBG_MEMALLOC_ALLOCATOR_DLMALLOC_DEBUG
128 do_check_chunk( struct malloc_chunk * );
131 do_check_free_chunk( struct malloc_chunk * );
134 do_check_inuse_chunk( struct malloc_chunk * );
137 do_check_malloced_chunk( struct malloc_chunk *, Cyg_dlmalloc_size_t );
141 // Constructor: gives the base and size of the arena in which memory is
142 // to be carved out, note that management structures are taken from the
144 Cyg_Mempool_dlmalloc_Implementation( cyg_uint8 * /* base */,
145 cyg_int32 /* size */,
146 CYG_ADDRWORD /* argthru */ );
149 //~Cyg_Mempool_dlmalloc_Implementation() {}
151 // get some memory, return NULL if none available
153 try_alloc( cyg_int32 /* size */ );
155 // resize existing allocation, if oldsize is non-NULL, previous
156 // allocation size is placed into it. If previous size not available,
157 // it is set to 0. NB previous allocation size may have been rounded up.
158 // Occasionally the allocation can be adjusted *backwards* as well as,
159 // or instead of forwards, therefore the address of the resized
160 // allocation is returned, or NULL if no resizing was possible.
161 // Note that this differs from ::realloc() in that no attempt is
162 // made to call malloc() if resizing is not possible - that is left
163 // to higher layers. The data is copied from old to new though.
164 // The effects of alloc_ptr==NULL or newsize==0 are undefined
166 resize_alloc( cyg_uint8 * /* alloc_ptr */, cyg_int32 /* newsize */,
167 cyg_int32 * /* oldsize */ );
169 // free the memory back to the pool
170 // returns true on success
172 free( cyg_uint8 * /* ptr */, cyg_int32 /* size */ =0 );
174 // Get memory pool status
175 // flags is a bitmask of requested fields to fill in. The flags are
176 // defined in common.hxx
178 get_status( cyg_mempool_status_flag_t /* flags */,
179 Cyg_Mempool_Status & /* status */ );
183 #endif // ifndef CYGONCE_MEMALLOC_DLMALLOCIMPL_HXX
184 // EOF dlmallocimpl.hxx