comparison packages/services/memalloc/common/current/src/kapi.cxx @ 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 //==========================================================================
2 //
3 // kapi.cxx
4 //
5 // Implementation of kernel C API functions for memory pools
6 //
7 //==========================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Red Hat eCos Public License
12 // Version 1.1 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://www.redhat.com/
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Configurable Operating System,
22 // released September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Red Hat.
25 // Portions created by Red Hat are
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
27 // All Rights Reserved.
28 // -------------------------------------------
29 //
30 //####COPYRIGHTEND####
31 //==========================================================================
32 //#####DESCRIPTIONBEGIN####
33 //
34 // Author(s): nickg, dsm, jlarmour
35 // Contributors:
36 // Date: 2000-06-12
37 // Description: Implementation of kernel C API functions for memory pools
38 // Usage:
39 //
40 //
41 //####DESCRIPTIONEND####
42 //
43 //==========================================================================
44
45 // CONFIGURATION
46
47 #include <pkgconf/memalloc.h>
48 #include <pkgconf/system.h>
49 #ifdef CYGPKG_KERNEL
50 # include <pkgconf/kernel.h>
51 #endif
52
53 #ifdef CYGFUN_MEMALLOC_KAPI
54
55 // INCLUDES
56
57 #include <cyg/infra/cyg_type.h> // types
58 #include <cyg/infra/cyg_ass.h> // assertion macros
59 #include <cyg/infra/cyg_trac.h> // tracing macros
60 #include <cyg/kernel/ktypes.h> // base kernel types
61
62 #include <cyg/memalloc/memvar.hxx>
63 #include <cyg/memalloc/memfixed.hxx>
64 #include <cyg/memalloc/common.hxx> // status flags
65
66 #include <cyg/kernel/kapi.h> // C API
67
68 // MACROS
69
70 #ifdef CYGDBG_USE_ASSERTS
71
72 #define CYG_ASSERT_SIZES(cstruct, cxxstruct) \
73 CYG_MACRO_START \
74 char *msg = "Size of C struct " #cstruct \
75 " != size of C++ struct " #cxxstruct ; \
76 CYG_ASSERT( sizeof(cstruct) == sizeof(cxxstruct) , msg ); \
77 CYG_MACRO_END
78
79 #else
80
81 #define CYG_ASSERT_SIZES(cstruct, cxxstruct)
82
83 #endif
84
85 // FUNCTIONS
86
87 // -------------------------------------------------------------------------
88 // Magic new function
89
90 inline void *operator new(size_t size, void *ptr)
91 {
92 CYG_CHECK_DATA_PTR( ptr, "Bad pointer" );
93 return ptr;
94 }
95
96 /*-----------------------------------------------------------------------*/
97 /* Memory pools */
98
99 /* Create a variable size memory pool */
100 externC void cyg_mempool_var_create(
101 void *base, /* base of memory to use for pool */
102 cyg_int32 size, /* size of memory in bytes */
103 cyg_handle_t *handle, /* returned handle of memory pool */
104 cyg_mempool_var *var /* space to put pool structure in */
105 )
106 {
107 CYG_ASSERT_SIZES( cyg_mempool_var, Cyg_Mempool_Variable );
108
109 Cyg_Mempool_Variable *t = new((void *)var) Cyg_Mempool_Variable (
110 (cyg_uint8 *)base,
111 size
112 );
113 t=t;
114
115 CYG_CHECK_DATA_PTR( handle, "Bad handle pointer" );
116 *handle = (cyg_handle_t)var;
117 }
118
119 /* Delete variable size memory pool */
120 externC void cyg_mempool_var_delete(cyg_handle_t varpool)
121 {
122 ((Cyg_Mempool_Variable *)varpool)->~Cyg_Mempool_Variable();
123 }
124
125 /* Allocates a block of length size. This waits if the memory is not
126 currently available. */
127 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_VARIABLE_THREADAWARE
128 externC void *cyg_mempool_var_alloc(cyg_handle_t varpool, cyg_int32 size)
129 {
130 return ((Cyg_Mempool_Variable *)varpool)->alloc(size);
131 }
132
133 # ifdef CYGFUN_KERNEL_THREADS_TIMER
134
135 /* Allocates a block of length size. This waits for up to delay
136 ticks, if the memory is not already available. NULL is returned if
137 no memory is available. */
138 externC void *cyg_mempool_var_timed_alloc(
139 cyg_handle_t varpool,
140 cyg_int32 size,
141 cyg_tick_count_t abstime)
142 {
143 return ((Cyg_Mempool_Variable *)varpool)->alloc(size, abstime);
144 }
145
146 # endif
147 #endif
148
149 /* Allocates a block of length size. NULL is returned if no memory is
150 available. */
151 externC void *cyg_mempool_var_try_alloc(
152 cyg_handle_t varpool,
153 cyg_int32 size)
154 {
155 return ((Cyg_Mempool_Variable *)varpool)->try_alloc(size);
156 }
157
158 /* Frees memory back into variable size pool. */
159 externC void cyg_mempool_var_free(cyg_handle_t varpool, void *p)
160 {
161 cyg_bool b;
162 b = ((Cyg_Mempool_Variable *)varpool)->free((cyg_uint8 *)p, 0);
163 CYG_ASSERT( b, "Bad free");
164 }
165
166
167 /* Returns true if there are any threads waiting for memory in the
168 given memory pool. */
169 externC cyg_bool_t cyg_mempool_var_waiting(cyg_handle_t varpool)
170 {
171 Cyg_Mempool_Variable *v = (Cyg_Mempool_Variable *)varpool;
172 Cyg_Mempool_Status stat;
173
174 v->get_status( CYG_MEMPOOL_STAT_WAITING, stat );
175 return (stat.waiting != 0);
176 }
177
178 /* Puts information about a variable memory pool into the structure
179 provided. */
180 externC void cyg_mempool_var_get_info(
181 cyg_handle_t varpool,
182 cyg_mempool_info *info)
183 {
184 Cyg_Mempool_Variable *v = (Cyg_Mempool_Variable *)varpool;
185 Cyg_Mempool_Status stat;
186
187 v->get_status( CYG_MEMPOOL_STAT_ARENASIZE|
188 CYG_MEMPOOL_STAT_TOTALFREE|
189 CYG_MEMPOOL_STAT_ARENABASE|
190 CYG_MEMPOOL_STAT_ORIGSIZE|
191 CYG_MEMPOOL_STAT_MAXFREE, stat );
192
193 info->totalmem = stat.arenasize;
194 info->freemem = stat.totalfree;
195 info->size = stat.origsize;
196 info->base = const_cast<cyg_uint8 *>(stat.arenabase);
197 info->blocksize = -1;
198 info->maxfree = stat.maxfree;
199 }
200
201
202 /* Create a fixed size memory pool */
203 externC void cyg_mempool_fix_create(
204 void *base, // base of memory to use for pool
205 cyg_int32 size, // size of memory in byte
206 cyg_int32 blocksize, // size of allocation in bytes
207 cyg_handle_t *handle, // handle of memory pool
208 cyg_mempool_fix *fix // space to put pool structure in
209 )
210 {
211 CYG_ASSERT_SIZES( cyg_mempool_fix, Cyg_Mempool_Fixed );
212
213 Cyg_Mempool_Fixed *t = new((void *)fix) Cyg_Mempool_Fixed (
214 (cyg_uint8 *)base,
215 size,
216 blocksize
217 );
218 t=t;
219
220 CYG_CHECK_DATA_PTR( handle, "Bad handle pointer" );
221 *handle = (cyg_handle_t)fix;
222 }
223
224 /* Delete fixed size memory pool */
225 externC void cyg_mempool_fix_delete(cyg_handle_t fixpool)
226 {
227 ((Cyg_Mempool_Fixed *)fixpool)->~Cyg_Mempool_Fixed();
228 }
229
230 #ifdef CYGSEM_MEMALLOC_ALLOCATOR_FIXED_THREADAWARE
231 /* Allocates a block. This waits if the memory is not
232 currently available. */
233 externC void *cyg_mempool_fix_alloc(cyg_handle_t fixpool)
234 {
235 return ((Cyg_Mempool_Fixed *)fixpool)->alloc();
236 }
237
238 # ifdef CYGFUN_KERNEL_THREADS_TIMER
239
240 /* Allocates a block. This waits for up to delay ticks, if the memory
241 is not already available. NULL is returned if no memory is
242 available. */
243 externC void *cyg_mempool_fix_timed_alloc(
244 cyg_handle_t fixpool,
245 cyg_tick_count_t abstime)
246 {
247 return ((Cyg_Mempool_Fixed *)fixpool)->alloc(abstime);
248 }
249
250 # endif
251 #endif
252
253 /* Allocates a block. NULL is returned if no memory is available. */
254 externC void *cyg_mempool_fix_try_alloc(cyg_handle_t fixpool)
255 {
256 return ((Cyg_Mempool_Fixed *)fixpool)->try_alloc();
257 }
258
259 /* Frees memory back into fixed size pool. */
260 externC void cyg_mempool_fix_free(cyg_handle_t fixpool, void *p)
261 {
262 cyg_bool b;
263 b = ((Cyg_Mempool_Fixed *)fixpool)->free((cyg_uint8 *)p);
264 CYG_ASSERT( b, "Bad free");
265 }
266
267 /* Returns true if there are any threads waiting for memory in the
268 given memory pool. */
269 externC cyg_bool_t cyg_mempool_fix_waiting(cyg_handle_t fixpool)
270 {
271 Cyg_Mempool_Fixed *f = (Cyg_Mempool_Fixed *)fixpool;
272 Cyg_Mempool_Status stat;
273
274 f->get_status( CYG_MEMPOOL_STAT_WAITING, stat );
275 return (stat.waiting != 0);
276 }
277
278 /* Puts information about a fixed block memory pool into the structure
279 provided. */
280 externC void cyg_mempool_fix_get_info(
281 cyg_handle_t fixpool,
282 cyg_mempool_info *info)
283 {
284 Cyg_Mempool_Fixed *f = (Cyg_Mempool_Fixed *)fixpool;
285 Cyg_Mempool_Status stat;
286
287 f->get_status( CYG_MEMPOOL_STAT_ARENASIZE|
288 CYG_MEMPOOL_STAT_TOTALFREE|
289 CYG_MEMPOOL_STAT_ARENABASE|
290 CYG_MEMPOOL_STAT_ORIGSIZE|
291 CYG_MEMPOOL_STAT_BLOCKSIZE|
292 CYG_MEMPOOL_STAT_MAXFREE, stat );
293
294 info->totalmem = stat.arenasize;
295 info->freemem = stat.totalfree;
296 info->size = stat.origsize;
297 info->base = const_cast<cyg_uint8 *>(stat.arenabase);
298 info->blocksize = stat.blocksize;
299 info->maxfree = stat.maxfree;
300 }
301
302 // -------------------------------------------------------------------------
303 // Check structure sizes.
304 // This class and constructor get run automatically in debug versions
305 // of the kernel and check that the structures configured in the C
306 // code are the same size as the C++ classes they should match.
307
308 #ifdef CYGPKG_INFRA_DEBUG
309
310 class Cyg_Check_Mem_Structure_Sizes
311 {
312 int dummy;
313 public:
314 Cyg_Check_Mem_Structure_Sizes( int x );
315
316 };
317
318 #define CYG_CHECK_SIZES(cstruct, cxxstruct) \
319 if( sizeof(cstruct) != sizeof(cxxstruct) ) \
320 { \
321 char *fmt = "Size of C struct " #cstruct \
322 " != size of C++ struct " #cxxstruct ; \
323 CYG_TRACE2(1, fmt, sizeof(cstruct) , sizeof(cxxstruct) ); \
324 fail = true; \
325 fmt = fmt; \
326 }
327
328 Cyg_Check_Mem_Structure_Sizes::Cyg_Check_Mem_Structure_Sizes(int x)
329 {
330 cyg_bool fail = false;
331
332 dummy = x+1;
333
334 CYG_CHECK_SIZES( cyg_mempool_var, Cyg_Mempool_Variable );
335 CYG_CHECK_SIZES( cyg_mempool_fix, Cyg_Mempool_Fixed );
336
337 CYG_ASSERT( !fail, "Size checks failed");
338 }
339
340 static Cyg_Check_Mem_Structure_Sizes cyg_memalloc_check_structure_sizes(1);
341
342 #endif
343
344 // -------------------------------------------------------------------------
345
346
347 #endif // ifdef CYGFUN_MEMALLOC_KAPI
348
349 // End of kapi.cxx