comparison packages/services/memalloc/common/current/include/dlmallocimpl.hxx @ 115:6ed91473a1cd ecos-sw-2000-08-21

Merge from eCos master repository on 2000-08-21-22:40:54-BST
author jlarmour
date Fri, 25 Aug 2000 17:32:38 +0000
parents
children e0c0827131d1
comparison
equal deleted inserted replaced
114:5ad2b71d525e 115:6ed91473a1cd
1 #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 //####COPYRIGHTBEGIN####
12 //
13 // -------------------------------------------
14 // The contents of this file are subject to the Red Hat eCos Public License
15 // Version 1.1 (the "License"); you may not use this file except in
16 // compliance with the License. You may obtain a copy of the License at
17 // http://www.redhat.com/
18 //
19 // Software distributed under the License is distributed on an "AS IS"
20 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
21 // License for the specific language governing rights and limitations under
22 // the License.
23 //
24 // The Original Code is eCos - Embedded Configurable Operating System,
25 // released September 30, 1998.
26 //
27 // The Initial Developer of the Original Code is Red Hat.
28 // Portions created by Red Hat are
29 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
30 // All Rights Reserved.
31 // -------------------------------------------
32 //
33 //####COPYRIGHTEND####
34 //==========================================================================
35 //#####DESCRIPTIONBEGIN####
36 //
37 // Author(s): jlarmour
38 // Contributors:
39 // Date: 2000-06-18
40 // Purpose: Define standard interface to Doug Lea's malloc implementation
41 // Description: Doug Lea's malloc has been ported to eCos. This file provides
42 // the interface between the implementation and the standard
43 // memory allocator interface required by eCos
44 // Usage: #include <cyg/memalloc/dlmalloc.hxx>
45 //
46 //
47 //####DESCRIPTIONEND####
48 //
49 //==========================================================================
50
51 // CONFIGURATION
52
53 #include <pkgconf/memalloc.h>
54
55 // INCLUDES
56
57 #include <stddef.h> // size_t, ptrdiff_t
58 #include <cyg/infra/cyg_type.h> // types
59
60 #include <cyg/memalloc/common.hxx> // Common memory allocator infra
61
62 // As a special case, override CYGIMP_MEMALLOC_ALLOCATOR_DLMALLOC_SAFE_MULTIPLE
63 // if the malloc config says so
64 #ifdef CYGIMP_MEMALLOC_MALLOC_DLMALLOC
65 // forward declaration to prevent header dependency problems
66 class Cyg_Mempool_dlmalloc;
67 # include <pkgconf/heaps.hxx>
68 # if (CYGMEM_HEAP_COUNT > 1) && \
69 !defined(CYGIMP_MEMALLOC_ALLOCATOR_DLMALLOC_SAFE_MULTIPLE)
70 # define CYGIMP_MEMALLOC_ALLOCATOR_DLMALLOC_SAFE_MULTIPLE 1
71 # endif
72 #endif
73
74 // CONSTANTS
75
76 // number of bins - but changing this alone will not change the number of
77 // bins!
78 #define CYGPRI_MEMALLOC_ALLOCATOR_DLMALLOC_NAV 128
79
80 // TYPE DEFINITIONS
81
82
83 class Cyg_Mempool_dlmalloc_Implementation
84 {
85 public:
86 /* cyg_dlmalloc_size_t is the word-size used for internal bookkeeping
87 of chunk sizes. On a 64-bit machine, you can reduce malloc
88 overhead, especially for very small chunks, by defining
89 cyg_dlmalloc_size_t to be a 32-bit type at the expense of not
90 being able to handle requests greater than 2^31. This limitation is
91 hardly ever a concern; you are encouraged to set this. However, the
92 default version is the same as size_t. */
93
94 typedef size_t Cyg_dlmalloc_size_t;
95
96 typedef struct malloc_chunk
97 {
98 Cyg_dlmalloc_size_t prev_size; /* Size of previous chunk (if free). */
99 Cyg_dlmalloc_size_t size; /* Size in bytes, including overhead. */
100 struct malloc_chunk* fd; /* double links -- used only if free. */
101 struct malloc_chunk* bk;
102 };
103
104 protected:
105 /* The first value returned from sbrk */
106 cyg_uint8 *arenabase;
107
108 /* The total memory in the pool */
109 cyg_int32 arenasize;
110
111 #ifdef CYGIMP_MEMALLOC_ALLOCATOR_DLMALLOC_SAFE_MULTIPLE
112 struct Cyg_Mempool_dlmalloc_Implementation::malloc_chunk *
113 av_[ CYGPRI_MEMALLOC_ALLOCATOR_DLMALLOC_NAV * 2 + 2 ];
114 #endif
115
116 #ifdef CYGDBG_MEMALLOC_ALLOCATOR_DLMALLOC_DEBUG
117
118 void
119 do_check_chunk( struct malloc_chunk * );
120
121 void
122 do_check_free_chunk( struct malloc_chunk * );
123
124 void
125 do_check_inuse_chunk( struct malloc_chunk * );
126
127 void
128 do_check_malloced_chunk( struct malloc_chunk *, Cyg_dlmalloc_size_t );
129 #endif
130
131 public:
132 // Constructor: gives the base and size of the arena in which memory is
133 // to be carved out, note that management structures are taken from the
134 // same arena.
135 Cyg_Mempool_dlmalloc_Implementation( cyg_uint8 * /* base */,
136 cyg_int32 /* size */,
137 CYG_ADDRWORD /* argthru */ );
138
139 // Destructor
140 ~Cyg_Mempool_dlmalloc_Implementation() {}
141
142 // get some memory, return NULL if none available
143 cyg_uint8 *
144 try_alloc( cyg_int32 /* size */ );
145
146 // resize existing allocation, if oldsize is non-NULL, previous
147 // allocation size is placed into it. If previous size not available,
148 // it is set to 0. NB previous allocation size may have been rounded up.
149 // Occasionally the allocation can be adjusted *backwards* as well as,
150 // or instead of forwards, therefore the address of the resized
151 // allocation is returned, or NULL if no resizing was possible.
152 // Note that this differs from ::realloc() in that no attempt is
153 // made to call malloc() if resizing is not possible - that is left
154 // to higher layers. The data is copied from old to new though.
155 // The effects of alloc_ptr==NULL or newsize==0 are undefined
156 cyg_uint8 *
157 resize_alloc( cyg_uint8 * /* alloc_ptr */, cyg_int32 /* newsize */,
158 cyg_int32 * /* oldsize */ );
159
160 // free the memory back to the pool
161 // returns true on success
162 cyg_bool
163 free( cyg_uint8 * /* ptr */, cyg_int32 /* size */ =0 );
164
165 // Get memory pool status
166 // flags is a bitmask of requested fields to fill in. The flags are
167 // defined in common.hxx
168 void
169 get_status( cyg_mempool_status_flag_t /* flags */,
170 Cyg_Mempool_Status & /* status */ );
171
172 };
173
174 #endif // ifndef CYGONCE_MEMALLOC_DLMALLOCIMPL_HXX
175 // EOF dlmallocimpl.hxx