Changes suggested by Adamo and Enrico.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1543 35acf78f-673a-0410-8e92-d51de3d6d3f4
master
gdisirio 2010-01-24 09:04:55 +00:00
parent edc56330a9
commit 86eb39129b
5 changed files with 34 additions and 30 deletions

View File

@ -47,9 +47,9 @@ typedef struct memory_heap MemoryHeap;
*/
struct heap_header {
union {
struct heap_header *h_next; /**< @brief Next block in free list. */
MemoryHeap *h_heap; /**< @brief Block owner heap. */
};
struct heap_header *next; /**< @brief Next block in free list. */
MemoryHeap *heap; /**< @brief Block owner heap. */
} h_u; /**< @brief Overlapped fields. */
size_t h_size; /**< @brief Size of the memory block. */
};

View File

@ -88,7 +88,7 @@ extern VTList vtlist;
--vtlist.vt_next->vt_time; \
while (!(vtp = vtlist.vt_next)->vt_time) { \
vtfunc_t fn = vtp->vt_func; \
vtp->vt_func = NULL; \
vtp->vt_func = (vtfunc_t)NULL; \
vtp->vt_next->vt_prev = (void *)&vtlist; \
(&vtlist)->vt_next = vtp->vt_next; \
fn(vtp->vt_par); \

View File

@ -53,7 +53,7 @@ static MemoryHeap default_heap;
*/
void heap_init(void) {
default_heap.h_provider = chCoreAlloc;
default_heap.h_free.h_next = NULL;
default_heap.h_free.h_u.next = (struct heap_header *)NULL;
default_heap.h_free.h_size = 0;
#if CH_USE_MUTEXES
chMtxInit(&default_heap.h_mtx);
@ -70,17 +70,17 @@ void heap_init(void) {
* @param[in] size heap size
*
* @note Both the heap buffer base and the heap size must be aligned to
* the @p align_t type size.
* the @p align_t type size.
*/
void chHeapInit(MemoryHeap *heapp, void *buf, size_t size) {
struct heap_header *hp;
chDbgCheck(MEM_IS_ALIGNED(buf) && MEM_IS_ALIGNED(size), "chHeapInit");
heapp->h_provider = NULL;
heapp->h_free.h_next = hp = buf;
heapp->h_provider = (memgetfunc_t)NULL;
heapp->h_free.h_u.next = hp = buf;
heapp->h_free.h_size = 0;
hp->h_next = NULL;
hp->h_u.next = NULL;
hp->h_size = size - sizeof(struct heap_header);
#if CH_USE_MUTEXES
chMtxInit(&heapp->h_mtx);
@ -113,8 +113,8 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) {
qp = &heapp->h_free;
H_LOCK(heapp);
while (qp->h_next != NULL) {
hp = qp->h_next;
while (qp->h_u.next != NULL) {
hp = qp->h_u.next;
if (hp->h_size >= size) {
if (hp->h_size < size + sizeof(struct heap_header)) {
/*
@ -122,19 +122,19 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) {
* requested size because the fragment would be too small to be
* useful.
*/
qp->h_next = hp->h_next;
qp->h_u.next = hp->h_u.next;
}
else {
/*
* Block bigger enough, must split it.
*/
fp = (void *)((uint8_t *)(hp) + sizeof(struct heap_header) + size);
fp->h_next = hp->h_next;
fp->h_u.next = hp->h_u.next;
fp->h_size = hp->h_size - sizeof(struct heap_header) - size;
qp->h_next = fp;
qp->h_u.next = fp;
hp->h_size = size;
}
hp->h_heap = heapp;
hp->h_u.heap = heapp;
H_UNLOCK(heapp);
return (void *)(hp + 1);
@ -150,7 +150,7 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) {
if (heapp->h_provider) {
hp = heapp->h_provider(size + sizeof(struct heap_header));
if (hp != NULL) {
hp->h_heap = heapp;
hp->h_u.heap = heapp;
hp->h_size = size;
hp++;
return (void *)hp;
@ -175,7 +175,7 @@ void chHeapFree(void *p) {
chDbgCheck(p != NULL, "chHeapFree");
hp = (struct heap_header *)p - 1;
heapp = hp->h_heap;
heapp = hp->h_u.heap;
qp = &heapp->h_free;
H_LOCK(heapp);
@ -185,32 +185,32 @@ void chHeapFree(void *p) {
"within free block");
if (((qp == &heapp->h_free) || (hp > qp)) &&
((qp->h_next == NULL) || (hp < qp->h_next))) {
((qp->h_u.next == NULL) || (hp < qp->h_u.next))) {
/*
* Insertion after qp.
*/
hp->h_next = qp->h_next;
qp->h_next = hp;
hp->h_u.next = qp->h_u.next;
qp->h_u.next = hp;
/*
* Verifies if the newly inserted block should be merged.
*/
if (LIMIT(hp) == hp->h_next) {
if (LIMIT(hp) == hp->h_u.next) {
/*
* Merge with the next block.
*/
hp->h_size += hp->h_next->h_size + sizeof(struct heap_header);
hp->h_next = hp->h_next->h_next;
hp->h_size += hp->h_u.next->h_size + sizeof(struct heap_header);
hp->h_u.next = hp->h_u.next->h_u.next;
}
if ((LIMIT(qp) == hp)) {
/*
* Merge with the previous block.
*/
qp->h_size += hp->h_size + sizeof(struct heap_header);
qp->h_next = hp->h_next;
qp->h_u.next = hp->h_u.next;
}
break;
}
qp = qp->h_next;
qp = qp->h_u.next;
}
H_UNLOCK(heapp);
@ -240,8 +240,8 @@ size_t chHeapStatus(MemoryHeap *heapp, size_t *sizep) {
H_LOCK(heapp);
sz = 0;
for (n = 0, qp = &heapp->h_free; qp->h_next; n++, qp = qp->h_next)
sz += qp->h_next->h_size;
for (n = 0, qp = &heapp->h_free; qp->h_u.next; n++, qp = qp->h_u.next)
sz += qp->h_u.next->h_size;
if (sizep)
*sizep = sz;

View File

@ -354,13 +354,17 @@ void chThdExit(msg_t msg) {
*/
msg_t chThdWait(Thread *tp) {
msg_t msg;
#if CH_USE_DYNAMIC
tmode_t mode;
#endif
chDbgCheck(tp != NULL, "chThdWait");
chSysLock();
chDbgAssert(tp != currp, "chThdWait(), #1", "waiting self");
chDbgAssert(tp->p_waiting == NULL, "chThdWait(), #2", "some other thread waiting");
chDbgAssert(tp->p_waiting == NULL, "chThdWait(), #2",
"some other thread waiting");
if (tp->p_state != THD_STATE_FINAL) {
tp->p_waiting = currp;
@ -373,7 +377,7 @@ msg_t chThdWait(Thread *tp) {
#else /* CH_USE_DYNAMIC */
/* Returning memory.*/
tmode_t mode = tp->p_flags & THD_MEM_MODE_MASK;
mode = tp->p_flags & THD_MEM_MODE_MASK;
chSysUnlock();
switch (mode) {

View File

@ -93,7 +93,7 @@ void chVTResetI(VirtualTimer *vtp) {
vtp->vt_next->vt_time += vtp->vt_time;
vtp->vt_prev->vt_next = vtp->vt_next;
vtp->vt_next->vt_prev = vtp->vt_prev;
vtp->vt_func = NULL;
vtp->vt_func = (vtfunc_t)NULL;
}
/**