/*-----------------------------------------------------------------------------+
|                                                                              |
|       Copyright (c), Raisonance S.A.S., 1987-2007                            |
|                                                                              |
+------------------------------------------------------------------------------+
|                                                                              |
|       File: ALLOC.H                                                          |
|                                                                              |
+--------------+-------+----------------------------------------------+--------+
|     DATE     |VERSION| HISTORY/CHANGES                              |   BY   |
|  (MM/DD/YY)  | VX.Y  |                                              |        |
+--------------+-------+----------------------------------------------+--------+
|   ??/??/??   |  1.0  | Initial implementation                       |   XX   |
|   11/07/02   |  1.1  | Reimplementation                             |   JR   |
|   30/03/06   |  1.2  | Use of alloc_size_t                          |   SC   |
+--------------+-------+----------------------------------------------+--------+
|Purpose    :                                                                  |
|   Memory management functions and variables                                  |
+-----------------------------------------------------------------------------*/

#ifndef _ALLOC_H_
#define _ALLOC_H_

#pragma SAVE

#define alloc_size_t size_t

/* include particular files at this point */
#if defined(__RC51__)
   #include <51\MAIN_C51.h>
#elif defined(__RCMX__)
   #include <MX\MAIN_MX.h>
#elif defined(__RCXA__)
   #include <XA\MAIN_XA.h>
#elif defined(__RCST5__)
   #include <ST5\MAIN_ST5.h>
#elif defined(__RCST6__)
   #include <ST6\MAIN_ST6.h>
#elif defined(__RCST7__)
   #include <ST7\MAIN_ST7.h>
#elif defined(__RCMP__)
   #include <MP\MAIN_MP.h>
#endif

#ifndef NULL
#define NULL 0L
#endif

/***********************************************************************
 * memory allocation structure.                                        *
 ***********************************************************************/
typedef struct dyn_alloc  {
  struct dyn_alloc allocmem *next  ;       /* pointer to next allocated block            */
  struct dyn_alloc allocmem *prev  ;       /* pointer to previous allocated block        */
  alloc_size_t               len   ;       /* length of current allocated block          */
  unsigned char              mem[2];       /* pointer returned to user (2 for alignment) */
} dynamic_allocator_struct;

/***********************************************************************
 * mempool_init initializes the memory pooler with the size 'size'.    *
 ***********************************************************************/
extern void mempool_init (dynamic_allocator_struct allocmem *p, alloc_size_t size) reentrant;

/***********************************************************************
 * calloc returns a pointer to space for an array of 'nobj' objects,   *
 * each of size 'size' or NULL if the request cannot be satisfied. The *
 * space is initialized to zero bytes.                                 *
 ***********************************************************************/
extern void * calloc(alloc_size_t nobj, alloc_size_t size) reentrant;

/***********************************************************************
 * free deallocates the space pointed to by p; it does nothing if p is *
 * NULL. p must be a pointer to space previously allocated by calloc,  *
 * malloc or realloc.                                                  *
 ***********************************************************************/
extern void free(void * p) reentrant;

/***********************************************************************
 * malloc returns a pointer to space for an object of size 'size', or  *
 * NULL if the request cannot be satisfied. The space is uninitialized *
 ***********************************************************************/
extern void * malloc(alloc_size_t size) reentrant;

/***********************************************************************
 * realloc changes the size of the object pointed to by p to 'size'.   *
 * The content will be unchanged up to the minimum of the old and new  *
 * sizes. If the new size is larger, the new space is uninitialized.   *
 * realloc returns a pointer to the new space, or NULL if the request  *
 * cannot be satisfied, in which case *p is unchanged.                 *
 ***********************************************************************/
extern void * realloc(void * p, alloc_size_t size) reentrant;

#define init_mempool mempool_init

#pragma RESTORE

#endif  /* _ALLOC_H_ */


