- if (!memPool->freePool) {
- new->m_next = 0;
- memPool->freePool = new;
- } else {
- /*
- * Find where this new chunk fits in the free list.
- */
- for (node = memPool->freePool; node && new > node; node = node->m_next)
- mprev = node;
- /*
- * Re-combine with the following free chunk if possible.
- */
- if ((((unsigned char *)new) + new->m_size) == (unsigned char *)node) {
- new->m_size += node->m_size;
- new->m_next = node->m_next;
- if (mprev) {
- if ((((unsigned char *)mprev) + mprev->m_size) == (unsigned char *)new) {
- mprev->m_size += new->m_size;
- mprev->m_next = new->m_next;
- } else
- mprev->m_next = new;
- } else
- memPool->freePool = new;
- /*
- * Re-combine with the previous free chunk if possible.
- */
- } else if (mprev && (((unsigned char *)mprev) + mprev->m_size) ==
- (unsigned char *)new) {
- mprev->m_size += new->m_size;
- if ((((unsigned char *)mprev) + mprev->m_size) == (unsigned char *)node) {
- mprev->m_size += node->m_size;
- mprev->m_next = node->m_next;
- } else
- mprev->m_next = node;
- /*
- * No luck re-combining, just insert the new chunk into the list.
- */
- } else {
- if (mprev)
- mprev->m_next = new;
- else
- memPool->freePool = new;
- new->m_next = node;
- }
- }