Changes suggested by Adamo and Enrico.
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1543 35acf78f-673a-0410-8e92-d51de3d6d3f4
This commit is contained in:
parent
edc56330a9
commit
86eb39129b
|
@ -47,9 +47,9 @@ typedef struct memory_heap MemoryHeap;
|
||||||
*/
|
*/
|
||||||
struct heap_header {
|
struct heap_header {
|
||||||
union {
|
union {
|
||||||
struct heap_header *h_next; /**< @brief Next block in free list. */
|
struct heap_header *next; /**< @brief Next block in free list. */
|
||||||
MemoryHeap *h_heap; /**< @brief Block owner heap. */
|
MemoryHeap *heap; /**< @brief Block owner heap. */
|
||||||
};
|
} h_u; /**< @brief Overlapped fields. */
|
||||||
size_t h_size; /**< @brief Size of the memory block. */
|
size_t h_size; /**< @brief Size of the memory block. */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -88,7 +88,7 @@ extern VTList vtlist;
|
||||||
--vtlist.vt_next->vt_time; \
|
--vtlist.vt_next->vt_time; \
|
||||||
while (!(vtp = vtlist.vt_next)->vt_time) { \
|
while (!(vtp = vtlist.vt_next)->vt_time) { \
|
||||||
vtfunc_t fn = vtp->vt_func; \
|
vtfunc_t fn = vtp->vt_func; \
|
||||||
vtp->vt_func = NULL; \
|
vtp->vt_func = (vtfunc_t)NULL; \
|
||||||
vtp->vt_next->vt_prev = (void *)&vtlist; \
|
vtp->vt_next->vt_prev = (void *)&vtlist; \
|
||||||
(&vtlist)->vt_next = vtp->vt_next; \
|
(&vtlist)->vt_next = vtp->vt_next; \
|
||||||
fn(vtp->vt_par); \
|
fn(vtp->vt_par); \
|
||||||
|
|
|
@ -53,7 +53,7 @@ static MemoryHeap default_heap;
|
||||||
*/
|
*/
|
||||||
void heap_init(void) {
|
void heap_init(void) {
|
||||||
default_heap.h_provider = chCoreAlloc;
|
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;
|
default_heap.h_free.h_size = 0;
|
||||||
#if CH_USE_MUTEXES
|
#if CH_USE_MUTEXES
|
||||||
chMtxInit(&default_heap.h_mtx);
|
chMtxInit(&default_heap.h_mtx);
|
||||||
|
@ -70,17 +70,17 @@ void heap_init(void) {
|
||||||
* @param[in] size heap size
|
* @param[in] size heap size
|
||||||
*
|
*
|
||||||
* @note Both the heap buffer base and the heap size must be aligned to
|
* @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) {
|
void chHeapInit(MemoryHeap *heapp, void *buf, size_t size) {
|
||||||
struct heap_header *hp;
|
struct heap_header *hp;
|
||||||
|
|
||||||
chDbgCheck(MEM_IS_ALIGNED(buf) && MEM_IS_ALIGNED(size), "chHeapInit");
|
chDbgCheck(MEM_IS_ALIGNED(buf) && MEM_IS_ALIGNED(size), "chHeapInit");
|
||||||
|
|
||||||
heapp->h_provider = NULL;
|
heapp->h_provider = (memgetfunc_t)NULL;
|
||||||
heapp->h_free.h_next = hp = buf;
|
heapp->h_free.h_u.next = hp = buf;
|
||||||
heapp->h_free.h_size = 0;
|
heapp->h_free.h_size = 0;
|
||||||
hp->h_next = NULL;
|
hp->h_u.next = NULL;
|
||||||
hp->h_size = size - sizeof(struct heap_header);
|
hp->h_size = size - sizeof(struct heap_header);
|
||||||
#if CH_USE_MUTEXES
|
#if CH_USE_MUTEXES
|
||||||
chMtxInit(&heapp->h_mtx);
|
chMtxInit(&heapp->h_mtx);
|
||||||
|
@ -113,8 +113,8 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) {
|
||||||
qp = &heapp->h_free;
|
qp = &heapp->h_free;
|
||||||
H_LOCK(heapp);
|
H_LOCK(heapp);
|
||||||
|
|
||||||
while (qp->h_next != NULL) {
|
while (qp->h_u.next != NULL) {
|
||||||
hp = qp->h_next;
|
hp = qp->h_u.next;
|
||||||
if (hp->h_size >= size) {
|
if (hp->h_size >= size) {
|
||||||
if (hp->h_size < size + sizeof(struct heap_header)) {
|
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
|
* requested size because the fragment would be too small to be
|
||||||
* useful.
|
* useful.
|
||||||
*/
|
*/
|
||||||
qp->h_next = hp->h_next;
|
qp->h_u.next = hp->h_u.next;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/*
|
/*
|
||||||
* Block bigger enough, must split it.
|
* Block bigger enough, must split it.
|
||||||
*/
|
*/
|
||||||
fp = (void *)((uint8_t *)(hp) + sizeof(struct heap_header) + size);
|
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;
|
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_size = size;
|
||||||
}
|
}
|
||||||
hp->h_heap = heapp;
|
hp->h_u.heap = heapp;
|
||||||
|
|
||||||
H_UNLOCK(heapp);
|
H_UNLOCK(heapp);
|
||||||
return (void *)(hp + 1);
|
return (void *)(hp + 1);
|
||||||
|
@ -150,7 +150,7 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) {
|
||||||
if (heapp->h_provider) {
|
if (heapp->h_provider) {
|
||||||
hp = heapp->h_provider(size + sizeof(struct heap_header));
|
hp = heapp->h_provider(size + sizeof(struct heap_header));
|
||||||
if (hp != NULL) {
|
if (hp != NULL) {
|
||||||
hp->h_heap = heapp;
|
hp->h_u.heap = heapp;
|
||||||
hp->h_size = size;
|
hp->h_size = size;
|
||||||
hp++;
|
hp++;
|
||||||
return (void *)hp;
|
return (void *)hp;
|
||||||
|
@ -175,7 +175,7 @@ void chHeapFree(void *p) {
|
||||||
chDbgCheck(p != NULL, "chHeapFree");
|
chDbgCheck(p != NULL, "chHeapFree");
|
||||||
|
|
||||||
hp = (struct heap_header *)p - 1;
|
hp = (struct heap_header *)p - 1;
|
||||||
heapp = hp->h_heap;
|
heapp = hp->h_u.heap;
|
||||||
qp = &heapp->h_free;
|
qp = &heapp->h_free;
|
||||||
H_LOCK(heapp);
|
H_LOCK(heapp);
|
||||||
|
|
||||||
|
@ -185,32 +185,32 @@ void chHeapFree(void *p) {
|
||||||
"within free block");
|
"within free block");
|
||||||
|
|
||||||
if (((qp == &heapp->h_free) || (hp > qp)) &&
|
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.
|
* Insertion after qp.
|
||||||
*/
|
*/
|
||||||
hp->h_next = qp->h_next;
|
hp->h_u.next = qp->h_u.next;
|
||||||
qp->h_next = hp;
|
qp->h_u.next = hp;
|
||||||
/*
|
/*
|
||||||
* Verifies if the newly inserted block should be merged.
|
* 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.
|
* Merge with the next block.
|
||||||
*/
|
*/
|
||||||
hp->h_size += hp->h_next->h_size + sizeof(struct heap_header);
|
hp->h_size += hp->h_u.next->h_size + sizeof(struct heap_header);
|
||||||
hp->h_next = hp->h_next->h_next;
|
hp->h_u.next = hp->h_u.next->h_u.next;
|
||||||
}
|
}
|
||||||
if ((LIMIT(qp) == hp)) {
|
if ((LIMIT(qp) == hp)) {
|
||||||
/*
|
/*
|
||||||
* Merge with the previous block.
|
* Merge with the previous block.
|
||||||
*/
|
*/
|
||||||
qp->h_size += hp->h_size + sizeof(struct heap_header);
|
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;
|
break;
|
||||||
}
|
}
|
||||||
qp = qp->h_next;
|
qp = qp->h_u.next;
|
||||||
}
|
}
|
||||||
|
|
||||||
H_UNLOCK(heapp);
|
H_UNLOCK(heapp);
|
||||||
|
@ -240,8 +240,8 @@ size_t chHeapStatus(MemoryHeap *heapp, size_t *sizep) {
|
||||||
H_LOCK(heapp);
|
H_LOCK(heapp);
|
||||||
|
|
||||||
sz = 0;
|
sz = 0;
|
||||||
for (n = 0, qp = &heapp->h_free; qp->h_next; n++, qp = qp->h_next)
|
for (n = 0, qp = &heapp->h_free; qp->h_u.next; n++, qp = qp->h_u.next)
|
||||||
sz += qp->h_next->h_size;
|
sz += qp->h_u.next->h_size;
|
||||||
if (sizep)
|
if (sizep)
|
||||||
*sizep = sz;
|
*sizep = sz;
|
||||||
|
|
||||||
|
|
|
@ -354,13 +354,17 @@ void chThdExit(msg_t msg) {
|
||||||
*/
|
*/
|
||||||
msg_t chThdWait(Thread *tp) {
|
msg_t chThdWait(Thread *tp) {
|
||||||
msg_t msg;
|
msg_t msg;
|
||||||
|
#if CH_USE_DYNAMIC
|
||||||
|
tmode_t mode;
|
||||||
|
#endif
|
||||||
|
|
||||||
chDbgCheck(tp != NULL, "chThdWait");
|
chDbgCheck(tp != NULL, "chThdWait");
|
||||||
|
|
||||||
chSysLock();
|
chSysLock();
|
||||||
|
|
||||||
chDbgAssert(tp != currp, "chThdWait(), #1", "waiting self");
|
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) {
|
if (tp->p_state != THD_STATE_FINAL) {
|
||||||
tp->p_waiting = currp;
|
tp->p_waiting = currp;
|
||||||
|
@ -373,7 +377,7 @@ msg_t chThdWait(Thread *tp) {
|
||||||
#else /* CH_USE_DYNAMIC */
|
#else /* CH_USE_DYNAMIC */
|
||||||
|
|
||||||
/* Returning memory.*/
|
/* Returning memory.*/
|
||||||
tmode_t mode = tp->p_flags & THD_MEM_MODE_MASK;
|
mode = tp->p_flags & THD_MEM_MODE_MASK;
|
||||||
chSysUnlock();
|
chSysUnlock();
|
||||||
|
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
|
|
|
@ -93,7 +93,7 @@ void chVTResetI(VirtualTimer *vtp) {
|
||||||
vtp->vt_next->vt_time += vtp->vt_time;
|
vtp->vt_next->vt_time += vtp->vt_time;
|
||||||
vtp->vt_prev->vt_next = vtp->vt_next;
|
vtp->vt_prev->vt_next = vtp->vt_next;
|
||||||
vtp->vt_next->vt_prev = vtp->vt_prev;
|
vtp->vt_next->vt_prev = vtp->vt_prev;
|
||||||
vtp->vt_func = NULL;
|
vtp->vt_func = (vtfunc_t)NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue