heap.c File Reference

VM Heap. More...

#include "pm.h"

Go to the source code of this file.

Classes

struct  PmHeapDesc_s
struct  PmHeap_s

Defines

#define __FILE_ID__   0x06
#define HEAP_NUM_TEMP_ROOTS   24
#define HEAP_MAX_LIVE_CHUNK_SIZE   2044
#define HEAP_MAX_FREE_CHUNK_SIZE   65532
#define HEAP_MIN_CHUNK_SIZE   ((sizeof(PmHeapDesc_t) + 3) & ~3)
#define OBJ_GET_GCVAL(pobj)   ((((pPmObj_t)pobj)->od >> OD_MARK_SHIFT) & 1)
#define OBJ_SET_GCVAL(pobj, gcval)

Typedefs

typedef struct PmHeapDesc_s PmHeapDesc_t
typedef struct PmHeapDesc_spPmHeapDesc_t
typedef struct PmHeap_s PmHeap_t
typedef struct PmHeap_s * pPmHeap_t

Functions

static PmReturn_t heap_unlinkFromFreelist (pPmHeapDesc_t pchunk)
static PmReturn_t heap_linkToFreelist (pPmHeapDesc_t pchunk)
PmReturn_t heap_init (void)
static PmReturn_t heap_getChunkImpl (uint16_t size, uint8_t **r_pchunk)
PmReturn_t heap_getChunk (uint16_t requestedsize, uint8_t **r_pchunk)
PmReturn_t heap_freeChunk (pPmObj_t ptr)
uint16_t heap_getAvail (void)
void heap_gcPushTempRoot (pPmObj_t pobj, uint8_t *r_objid)
void heap_gcPopTempRoot (uint8_t objid)

Variables

static PmHeap_t pmHeap PM_PLAT_HEAP_ATTR


Detailed Description

VM Heap.

VM heap operations. All of PyMite's dynamic memory is obtained from this heap. The heap provides dynamic memory on demand.

Definition in file heap.c.


Define Documentation

#define HEAP_MAX_FREE_CHUNK_SIZE   65532

The maximum size a free chunk can be (a free chunk is one that is not in use). The free chunk size is limited by the size field in the *heap* descriptor. That field is fourteen bits with two assumed least significant bits (zeros): (0x3FFF << 2) == 65532

Definition at line 58 of file heap.c.

#define HEAP_MAX_LIVE_CHUNK_SIZE   2044

The maximum size a live chunk can be (a live chunk is one that is in use). The live chunk size is limited by the size field in the *object* descriptor. That field is nine bits with two assumed least significant bits (zeros): (0x1FF << 2) == 2044

Definition at line 50 of file heap.c.

#define HEAP_MIN_CHUNK_SIZE   ((sizeof(PmHeapDesc_t) + 3) & ~3)

The minimum size a chunk can be (rounded up to a multiple of 4)

Definition at line 61 of file heap.c.

#define HEAP_NUM_TEMP_ROOTS   24

Checks for heap size definition. The size of the temporary roots stack

Definition at line 42 of file heap.c.

#define OBJ_GET_GCVAL ( pobj   )     ((((pPmObj_t)pobj)->od >> OD_MARK_SHIFT) & 1)

Gets the GC's mark bit for the object. This MUST NOT be called on objects that are free.

Definition at line 68 of file heap.c.

#define OBJ_SET_GCVAL ( pobj,
gcval   ) 

Sets the GC's mark bit for the object This MUST NOT be called on objects that are free.

Definition at line 83 of file heap.c.


Typedef Documentation

typedef struct PmHeapDesc_s PmHeapDesc_t

The following is a diagram of the heap descriptor at the head of the chunk:

 *                MSb          LSb
 *                7 6 5 4 3 2 1 0
 *      pchunk-> +-+-+-+-+-+-+-+-+
 *               |     S[9:2]    |     S := Size of the chunk (2 LSbs dropped)
 *               +-+-+-----------+     F := Chunk free bit (not in use)
 *               |F|R| S[15:10]  |     R := Bit reserved for future use
 *               +-+-+-----------+
 *               |     P(L)      |     P := hd_prev: Pointer to previous node
 *               |     P(H)      |     N := hd_next: Pointer to next node
 *               |     N(L)      |
 *               |     N(H)      |     Theoretical min size == 6
 *               +---------------+     Effective min size == 8
 *               | unused space  |     (12 on 32-bit MCUs)
 *               ...           ...
 *               | end chunk     |
 *               +---------------+
 * 


Function Documentation

PmReturn_t heap_freeChunk ( pPmObj_t  ptr  ) 

Places the chunk back in the heap.

Parameters:
ptr Pointer to object to free.

Definition at line 549 of file heap.c.

void heap_gcPopTempRoot ( uint8_t  objid  ) 

Pops from the temporary roots stack all objects upto and including the one denoted by the given ID

Parameters:
objid ID of object to pop

Definition at line 1184 of file heap.c.

void heap_gcPushTempRoot ( pPmObj_t  pobj,
uint8_t r_objid 
)

Pushes an object onto the temporary roots stack if there is room to protect the objects from a potential garbage collection

Parameters:
pobj Object to push onto the roots stack
r_objid By reference; ID to use when popping the object from the stack

Definition at line 1183 of file heap.c.

uint16_t heap_getAvail ( void   ) 

Returns:
Return number of bytes available in the heap

Definition at line 578 of file heap.c.

PmReturn_t heap_getChunk ( uint16_t  requestedsize,
uint8_t **  r_pchunk 
)

Returns a free chunk from the heap.

The chunk will be at least the requested size. The actual size can be found in the return chunk's od.od_size.

Parameters:
requestedsize Requested size of the chunk in bytes.
r_pchunk Addr of ptr to chunk (return).
Returns:
Return code

Definition at line 498 of file heap.c.

static PmReturn_t heap_getChunkImpl ( uint16_t  size,
uint8_t **  r_pchunk 
) [static]

Obtains a chunk of memory from the free list

Performs the Best Fit algorithm. Iterates through the freelist to see if a chunk of suitable size exists. Shaves a chunk to perfect size iff the remainder is greater than the minimum chunk size.

Parameters:
size Requested chunk size
r_pchunk Return ptr to chunk
Returns:
Return status

Definition at line 419 of file heap.c.

PmReturn_t heap_init ( void   ) 

Initializes the heap for use.

Returns:
nothing.

Definition at line 352 of file heap.c.


Variable Documentation

PmHeap_t pmHeap PM_PLAT_HEAP_ATTR [static]

The PyMite heap

Definition at line 160 of file heap.c.


Generated on Mon Oct 18 07:40:49 2010 for Python-on-a-chip by  doxygen 1.5.9