1 |
1254 |
phoenix |
#ifndef CYGONCE_MEMALLOC_DLMALLOCIMPL_HXX
|
2 |
|
|
#define CYGONCE_MEMALLOC_DLMALLOCIMPL_HXX
|
3 |
|
|
|
4 |
|
|
//==========================================================================
|
5 |
|
|
//
|
6 |
|
|
// dlmallocimpl.hxx
|
7 |
|
|
//
|
8 |
|
|
// Interface to the port of Doug Lea's malloc implementation
|
9 |
|
|
//
|
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.
|
15 |
|
|
//
|
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.
|
19 |
|
|
//
|
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
|
23 |
|
|
// for more details.
|
24 |
|
|
//
|
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.
|
28 |
|
|
//
|
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.
|
35 |
|
|
//
|
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.
|
38 |
|
|
//
|
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####
|
45 |
|
|
//
|
46 |
|
|
// Author(s): jlarmour
|
47 |
|
|
// Contributors:
|
48 |
|
|
// Date: 2000-06-18
|
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
|
54 |
|
|
//
|
55 |
|
|
//
|
56 |
|
|
//####DESCRIPTIONEND####
|
57 |
|
|
//
|
58 |
|
|
//==========================================================================
|
59 |
|
|
|
60 |
|
|
// CONFIGURATION
|
61 |
|
|
|
62 |
|
|
#include
|
63 |
|
|
|
64 |
|
|
// INCLUDES
|
65 |
|
|
|
66 |
|
|
#include // size_t, ptrdiff_t
|
67 |
|
|
#include // types
|
68 |
|
|
|
69 |
|
|
#include // Common memory allocator infra
|
70 |
|
|
|
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
|
77 |
|
|
# if (CYGMEM_HEAP_COUNT > 1) && \
|
78 |
|
|
!defined(CYGIMP_MEMALLOC_ALLOCATOR_DLMALLOC_SAFE_MULTIPLE)
|
79 |
|
|
# define CYGIMP_MEMALLOC_ALLOCATOR_DLMALLOC_SAFE_MULTIPLE 1
|
80 |
|
|
# endif
|
81 |
|
|
#endif
|
82 |
|
|
|
83 |
|
|
// CONSTANTS
|
84 |
|
|
|
85 |
|
|
// number of bins - but changing this alone will not change the number of
|
86 |
|
|
// bins!
|
87 |
|
|
#define CYGPRI_MEMALLOC_ALLOCATOR_DLMALLOC_NAV 128
|
88 |
|
|
|
89 |
|
|
// TYPE DEFINITIONS
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
class Cyg_Mempool_dlmalloc_Implementation
|
93 |
|
|
{
|
94 |
|
|
public:
|
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. */
|
102 |
|
|
|
103 |
|
|
typedef size_t Cyg_dlmalloc_size_t;
|
104 |
|
|
|
105 |
|
|
typedef struct malloc_chunk
|
106 |
|
|
{
|
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;
|
111 |
|
|
};
|
112 |
|
|
|
113 |
|
|
protected:
|
114 |
|
|
/* The first value returned from sbrk */
|
115 |
|
|
cyg_uint8 *arenabase;
|
116 |
|
|
|
117 |
|
|
/* The total memory in the pool */
|
118 |
|
|
cyg_int32 arenasize;
|
119 |
|
|
|
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 ];
|
123 |
|
|
#endif
|
124 |
|
|
|
125 |
|
|
#ifdef CYGDBG_MEMALLOC_ALLOCATOR_DLMALLOC_DEBUG
|
126 |
|
|
|
127 |
|
|
void
|
128 |
|
|
do_check_chunk( struct malloc_chunk * );
|
129 |
|
|
|
130 |
|
|
void
|
131 |
|
|
do_check_free_chunk( struct malloc_chunk * );
|
132 |
|
|
|
133 |
|
|
void
|
134 |
|
|
do_check_inuse_chunk( struct malloc_chunk * );
|
135 |
|
|
|
136 |
|
|
void
|
137 |
|
|
do_check_malloced_chunk( struct malloc_chunk *, Cyg_dlmalloc_size_t );
|
138 |
|
|
#endif
|
139 |
|
|
|
140 |
|
|
public:
|
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
|
143 |
|
|
// same arena.
|
144 |
|
|
Cyg_Mempool_dlmalloc_Implementation( cyg_uint8 * /* base */,
|
145 |
|
|
cyg_int32 /* size */,
|
146 |
|
|
CYG_ADDRWORD /* argthru */ );
|
147 |
|
|
|
148 |
|
|
// Destructor
|
149 |
|
|
~Cyg_Mempool_dlmalloc_Implementation() {}
|
150 |
|
|
|
151 |
|
|
// get some memory, return NULL if none available
|
152 |
|
|
cyg_uint8 *
|
153 |
|
|
try_alloc( cyg_int32 /* size */ );
|
154 |
|
|
|
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
|
165 |
|
|
cyg_uint8 *
|
166 |
|
|
resize_alloc( cyg_uint8 * /* alloc_ptr */, cyg_int32 /* newsize */,
|
167 |
|
|
cyg_int32 * /* oldsize */ );
|
168 |
|
|
|
169 |
|
|
// free the memory back to the pool
|
170 |
|
|
// returns true on success
|
171 |
|
|
cyg_bool
|
172 |
|
|
free( cyg_uint8 * /* ptr */, cyg_int32 /* size */ =0 );
|
173 |
|
|
|
174 |
|
|
// Get memory pool status
|
175 |
|
|
// flags is a bitmask of requested fields to fill in. The flags are
|
176 |
|
|
// defined in common.hxx
|
177 |
|
|
void
|
178 |
|
|
get_status( cyg_mempool_status_flag_t /* flags */,
|
179 |
|
|
Cyg_Mempool_Status & /* status */ );
|
180 |
|
|
|
181 |
|
|
};
|
182 |
|
|
|
183 |
|
|
#endif // ifndef CYGONCE_MEMALLOC_DLMALLOCIMPL_HXX
|
184 |
|
|
// EOF dlmallocimpl.hxx
|