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