loader: cstyle cleanup libsa zalloc sources
Clean up libstand zalloc* sources. Note that it is not 100% whitespace cleanup. I also reduced block in znalloc and zfree as those were obvious simplifications and did help to save one level of indent.
This commit is contained in:
parent
144c4ca039
commit
e57c0c2afb
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* This module derived from code donated to the FreeBSD Project by
|
* This module derived from code donated to the FreeBSD Project by
|
||||||
* Matthew Dillon <dillon@backplane.com>
|
* Matthew Dillon <dillon@backplane.com>
|
||||||
*
|
*
|
||||||
* Copyright (c) 1998 The FreeBSD Project
|
* Copyright (c) 1998 The FreeBSD Project
|
||||||
@ -31,10 +31,10 @@
|
|||||||
__FBSDID("$FreeBSD$");
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* LIB/MEMORY/ZALLOC.C - self contained low-overhead memory pool/allocation
|
* LIB/MEMORY/ZALLOC.C - self contained low-overhead memory pool/allocation
|
||||||
* subsystem
|
* subsystem
|
||||||
*
|
*
|
||||||
* This subsystem implements memory pools and memory allocation
|
* This subsystem implements memory pools and memory allocation
|
||||||
* routines.
|
* routines.
|
||||||
*
|
*
|
||||||
* Pools are managed via a linked list of 'free' areas. Allocating
|
* Pools are managed via a linked list of 'free' areas. Allocating
|
||||||
@ -43,7 +43,7 @@ __FBSDID("$FreeBSD$");
|
|||||||
* to allocate the entire pool without incuring any structural overhead.
|
* to allocate the entire pool without incuring any structural overhead.
|
||||||
*
|
*
|
||||||
* The system works best when allocating similarly-sized chunks of
|
* The system works best when allocating similarly-sized chunks of
|
||||||
* memory. Care must be taken to avoid fragmentation when
|
* memory. Care must be taken to avoid fragmentation when
|
||||||
* allocating/deallocating dissimilar chunks.
|
* allocating/deallocating dissimilar chunks.
|
||||||
*
|
*
|
||||||
* When a memory pool is first allocated, the entire pool is marked as
|
* When a memory pool is first allocated, the entire pool is marked as
|
||||||
@ -53,7 +53,7 @@ __FBSDID("$FreeBSD$");
|
|||||||
* available.
|
* available.
|
||||||
*
|
*
|
||||||
* z[n]xalloc() works like z[n]alloc() but the allocation is made from
|
* z[n]xalloc() works like z[n]alloc() but the allocation is made from
|
||||||
* within the specified address range. If the segment could not be
|
* within the specified address range. If the segment could not be
|
||||||
* allocated, NULL is returned. WARNING! The address range will be
|
* allocated, NULL is returned. WARNING! The address range will be
|
||||||
* aligned to an 8 or 16 byte boundry depending on the cpu so if you
|
* aligned to an 8 or 16 byte boundry depending on the cpu so if you
|
||||||
* give an unaligned address range, unexpected results may occur.
|
* give an unaligned address range, unexpected results may occur.
|
||||||
@ -88,56 +88,54 @@ typedef char assert_align[(sizeof(struct MemNode) <= MALLOCALIGN) ? 1 : -1];
|
|||||||
void *
|
void *
|
||||||
znalloc(MemPool *mp, uintptr_t bytes)
|
znalloc(MemPool *mp, uintptr_t bytes)
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
* align according to pool object size (can be 0). This is
|
|
||||||
* inclusive of the MEMNODE_SIZE_MASK minimum alignment.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
bytes = (bytes + MEMNODE_SIZE_MASK) & ~MEMNODE_SIZE_MASK;
|
|
||||||
|
|
||||||
if (bytes == 0)
|
|
||||||
return((void *)-1);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* locate freelist entry big enough to hold the object. If all objects
|
|
||||||
* are the same size, this is a constant-time function.
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (bytes <= mp->mp_Size - mp->mp_Used) {
|
|
||||||
MemNode **pmn;
|
MemNode **pmn;
|
||||||
MemNode *mn;
|
MemNode *mn;
|
||||||
|
|
||||||
for (pmn = &mp->mp_First; (mn=*pmn) != NULL; pmn = &mn->mr_Next) {
|
/*
|
||||||
if (bytes > mn->mr_Bytes)
|
* align according to pool object size (can be 0). This is
|
||||||
continue;
|
* inclusive of the MEMNODE_SIZE_MASK minimum alignment.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
bytes = (bytes + MEMNODE_SIZE_MASK) & ~MEMNODE_SIZE_MASK;
|
||||||
|
|
||||||
/*
|
if (bytes == 0)
|
||||||
* Cut a chunk of memory out of the beginning of this
|
return ((void *)-1);
|
||||||
* block and fixup the link appropriately.
|
|
||||||
*/
|
|
||||||
|
|
||||||
{
|
/*
|
||||||
|
* locate freelist entry big enough to hold the object. If all objects
|
||||||
|
* are the same size, this is a constant-time function.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (bytes > mp->mp_Size - mp->mp_Used)
|
||||||
|
return (NULL);
|
||||||
|
|
||||||
|
for (pmn = &mp->mp_First; (mn = *pmn) != NULL; pmn = &mn->mr_Next) {
|
||||||
char *ptr = (char *)mn;
|
char *ptr = (char *)mn;
|
||||||
|
|
||||||
|
if (bytes > mn->mr_Bytes)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Cut a chunk of memory out of the beginning of this
|
||||||
|
* block and fixup the link appropriately.
|
||||||
|
*/
|
||||||
if (mn->mr_Bytes == bytes) {
|
if (mn->mr_Bytes == bytes) {
|
||||||
*pmn = mn->mr_Next;
|
*pmn = mn->mr_Next;
|
||||||
} else {
|
} else {
|
||||||
mn = (MemNode *)((char *)mn + bytes);
|
mn = (MemNode *)((char *)mn + bytes);
|
||||||
mn->mr_Next = ((MemNode *)ptr)->mr_Next;
|
mn->mr_Next = ((MemNode *)ptr)->mr_Next;
|
||||||
mn->mr_Bytes = ((MemNode *)ptr)->mr_Bytes - bytes;
|
mn->mr_Bytes = ((MemNode *)ptr)->mr_Bytes - bytes;
|
||||||
*pmn = mn;
|
*pmn = mn;
|
||||||
}
|
}
|
||||||
mp->mp_Used += bytes;
|
mp->mp_Used += bytes;
|
||||||
return(ptr);
|
return(ptr);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Memory pool is full, return NULL.
|
* Memory pool is full, return NULL.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
return(NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -147,99 +145,97 @@ znalloc(MemPool *mp, uintptr_t bytes)
|
|||||||
void
|
void
|
||||||
zfree(MemPool *mp, void *ptr, uintptr_t bytes)
|
zfree(MemPool *mp, void *ptr, uintptr_t bytes)
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
* align according to pool object size (can be 0). This is
|
|
||||||
* inclusive of the MEMNODE_SIZE_MASK minimum alignment.
|
|
||||||
*/
|
|
||||||
bytes = (bytes + MEMNODE_SIZE_MASK) & ~MEMNODE_SIZE_MASK;
|
|
||||||
|
|
||||||
if (bytes == 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* panic if illegal pointer
|
|
||||||
*/
|
|
||||||
|
|
||||||
if ((char *)ptr < (char *)mp->mp_Base ||
|
|
||||||
(char *)ptr + bytes > (char *)mp->mp_End ||
|
|
||||||
((uintptr_t)ptr & MEMNODE_SIZE_MASK) != 0)
|
|
||||||
panic("zfree(%p,%ju): wild pointer", ptr, (uintmax_t)bytes);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* free the segment
|
|
||||||
*/
|
|
||||||
|
|
||||||
{
|
|
||||||
MemNode **pmn;
|
MemNode **pmn;
|
||||||
MemNode *mn;
|
MemNode *mn;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* align according to pool object size (can be 0). This is
|
||||||
|
* inclusive of the MEMNODE_SIZE_MASK minimum alignment.
|
||||||
|
*/
|
||||||
|
bytes = (bytes + MEMNODE_SIZE_MASK) & ~MEMNODE_SIZE_MASK;
|
||||||
|
|
||||||
|
if (bytes == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* panic if illegal pointer
|
||||||
|
*/
|
||||||
|
|
||||||
|
if ((char *)ptr < (char *)mp->mp_Base ||
|
||||||
|
(char *)ptr + bytes > (char *)mp->mp_End ||
|
||||||
|
((uintptr_t)ptr & MEMNODE_SIZE_MASK) != 0)
|
||||||
|
panic("zfree(%p,%ju): wild pointer", ptr, (uintmax_t)bytes);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* free the segment
|
||||||
|
*/
|
||||||
mp->mp_Used -= bytes;
|
mp->mp_Used -= bytes;
|
||||||
|
|
||||||
for (pmn = &mp->mp_First; (mn = *pmn) != NULL; pmn = &mn->mr_Next) {
|
for (pmn = &mp->mp_First; (mn = *pmn) != NULL; pmn = &mn->mr_Next) {
|
||||||
/*
|
|
||||||
* If area between last node and current node
|
|
||||||
* - check range
|
|
||||||
* - check merge with next area
|
|
||||||
* - check merge with previous area
|
|
||||||
*/
|
|
||||||
if ((char *)ptr <= (char *)mn) {
|
|
||||||
/*
|
/*
|
||||||
* range check
|
* If area between last node and current node
|
||||||
|
* - check range
|
||||||
|
* - check merge with next area
|
||||||
|
* - check merge with previous area
|
||||||
*/
|
*/
|
||||||
if ((char *)ptr + bytes > (char *)mn) {
|
if ((char *)ptr <= (char *)mn) {
|
||||||
panic("zfree(%p,%ju): corrupt memlist1", ptr,
|
/*
|
||||||
(uintmax_t)bytes);
|
* range check
|
||||||
|
*/
|
||||||
|
if ((char *)ptr + bytes > (char *)mn) {
|
||||||
|
panic("zfree(%p,%ju): corrupt memlist1", ptr,
|
||||||
|
(uintmax_t)bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* merge against next area or create independant area
|
||||||
|
*/
|
||||||
|
|
||||||
|
if ((char *)ptr + bytes == (char *)mn) {
|
||||||
|
((MemNode *)ptr)->mr_Next = mn->mr_Next;
|
||||||
|
((MemNode *)ptr)->mr_Bytes =
|
||||||
|
bytes + mn->mr_Bytes;
|
||||||
|
} else {
|
||||||
|
((MemNode *)ptr)->mr_Next = mn;
|
||||||
|
((MemNode *)ptr)->mr_Bytes = bytes;
|
||||||
|
}
|
||||||
|
*pmn = mn = (MemNode *)ptr;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* merge against previous area (if there is a previous
|
||||||
|
* area).
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (pmn != &mp->mp_First) {
|
||||||
|
if ((char *)pmn + ((MemNode*)pmn)->mr_Bytes ==
|
||||||
|
(char *)ptr) {
|
||||||
|
((MemNode *)pmn)->mr_Next = mn->mr_Next;
|
||||||
|
((MemNode *)pmn)->mr_Bytes +=
|
||||||
|
mn->mr_Bytes;
|
||||||
|
mn = (MemNode *)pmn;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
if ((char *)ptr < (char *)mn + mn->mr_Bytes) {
|
||||||
/*
|
panic("zfree(%p,%ju): corrupt memlist2", ptr,
|
||||||
* merge against next area or create independant area
|
(uintmax_t)bytes);
|
||||||
*/
|
|
||||||
|
|
||||||
if ((char *)ptr + bytes == (char *)mn) {
|
|
||||||
((MemNode *)ptr)->mr_Next = mn->mr_Next;
|
|
||||||
((MemNode *)ptr)->mr_Bytes= bytes + mn->mr_Bytes;
|
|
||||||
} else {
|
|
||||||
((MemNode *)ptr)->mr_Next = mn;
|
|
||||||
((MemNode *)ptr)->mr_Bytes= bytes;
|
|
||||||
}
|
}
|
||||||
*pmn = mn = (MemNode *)ptr;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* merge against previous area (if there is a previous
|
|
||||||
* area).
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (pmn != &mp->mp_First) {
|
|
||||||
if ((char*)pmn + ((MemNode*)pmn)->mr_Bytes == (char*)ptr) {
|
|
||||||
((MemNode *)pmn)->mr_Next = mn->mr_Next;
|
|
||||||
((MemNode *)pmn)->mr_Bytes += mn->mr_Bytes;
|
|
||||||
mn = (MemNode *)pmn;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
/* NOT REACHED */
|
|
||||||
}
|
|
||||||
if ((char *)ptr < (char *)mn + mn->mr_Bytes) {
|
|
||||||
panic("zfree(%p,%ju): corrupt memlist2", ptr,
|
|
||||||
(uintmax_t)bytes);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* We are beyond the last MemNode, append new MemNode. Merge against
|
* We are beyond the last MemNode, append new MemNode. Merge against
|
||||||
* previous area if possible.
|
* previous area if possible.
|
||||||
*/
|
*/
|
||||||
if (pmn == &mp->mp_First ||
|
if (pmn == &mp->mp_First ||
|
||||||
(char *)pmn + ((MemNode *)pmn)->mr_Bytes != (char *)ptr
|
(char *)pmn + ((MemNode *)pmn)->mr_Bytes != (char *)ptr) {
|
||||||
) {
|
((MemNode *)ptr)->mr_Next = NULL;
|
||||||
((MemNode *)ptr)->mr_Next = NULL;
|
((MemNode *)ptr)->mr_Bytes = bytes;
|
||||||
((MemNode *)ptr)->mr_Bytes = bytes;
|
*pmn = (MemNode *)ptr;
|
||||||
*pmn = (MemNode *)ptr;
|
mn = (MemNode *)ptr;
|
||||||
mn = (MemNode *)ptr;
|
|
||||||
} else {
|
} else {
|
||||||
((MemNode *)pmn)->mr_Bytes += bytes;
|
((MemNode *)pmn)->mr_Bytes += bytes;
|
||||||
mn = (MemNode *)pmn;
|
mn = (MemNode *)pmn;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -256,26 +252,26 @@ zfree(MemPool *mp, void *ptr, uintptr_t bytes)
|
|||||||
void
|
void
|
||||||
zextendPool(MemPool *mp, void *base, uintptr_t bytes)
|
zextendPool(MemPool *mp, void *base, uintptr_t bytes)
|
||||||
{
|
{
|
||||||
if (mp->mp_Size == 0) {
|
if (mp->mp_Size == 0) {
|
||||||
mp->mp_Base = base;
|
mp->mp_Base = base;
|
||||||
mp->mp_Used = bytes;
|
mp->mp_Used = bytes;
|
||||||
mp->mp_End = (char *)base + bytes;
|
mp->mp_End = (char *)base + bytes;
|
||||||
mp->mp_Size = bytes;
|
mp->mp_Size = bytes;
|
||||||
} else {
|
} else {
|
||||||
void *pend = (char *)mp->mp_Base + mp->mp_Size;
|
void *pend = (char *)mp->mp_Base + mp->mp_Size;
|
||||||
|
|
||||||
if (base < mp->mp_Base) {
|
if (base < mp->mp_Base) {
|
||||||
mp->mp_Size += (char *)mp->mp_Base - (char *)base;
|
mp->mp_Size += (char *)mp->mp_Base - (char *)base;
|
||||||
mp->mp_Used += (char *)mp->mp_Base - (char *)base;
|
mp->mp_Used += (char *)mp->mp_Base - (char *)base;
|
||||||
mp->mp_Base = base;
|
mp->mp_Base = base;
|
||||||
|
}
|
||||||
|
base = (char *)base + bytes;
|
||||||
|
if (base > pend) {
|
||||||
|
mp->mp_Size += (char *)base - (char *)pend;
|
||||||
|
mp->mp_Used += (char *)base - (char *)pend;
|
||||||
|
mp->mp_End = (char *)base;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
base = (char *)base + bytes;
|
|
||||||
if (base > pend) {
|
|
||||||
mp->mp_Size += (char *)base - (char *)pend;
|
|
||||||
mp->mp_Used += (char *)base - (char *)pend;
|
|
||||||
mp->mp_End = (char *)base;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ZALLOCDEBUG
|
#ifdef ZALLOCDEBUG
|
||||||
@ -283,34 +279,32 @@ zextendPool(MemPool *mp, void *base, uintptr_t bytes)
|
|||||||
void
|
void
|
||||||
zallocstats(MemPool *mp)
|
zallocstats(MemPool *mp)
|
||||||
{
|
{
|
||||||
int abytes = 0;
|
int abytes = 0;
|
||||||
int hbytes = 0;
|
int hbytes = 0;
|
||||||
int fcount = 0;
|
int fcount = 0;
|
||||||
MemNode *mn;
|
MemNode *mn;
|
||||||
|
|
||||||
printf("%d bytes reserved", (int) mp->mp_Size);
|
printf("%d bytes reserved", (int)mp->mp_Size);
|
||||||
|
|
||||||
mn = mp->mp_First;
|
mn = mp->mp_First;
|
||||||
|
|
||||||
if ((void *)mn != (void *)mp->mp_Base) {
|
if ((void *)mn != (void *)mp->mp_Base) {
|
||||||
abytes += (char *)mn - (char *)mp->mp_Base;
|
abytes += (char *)mn - (char *)mp->mp_Base;
|
||||||
}
|
|
||||||
|
|
||||||
while (mn) {
|
|
||||||
if ((char *)mn + mn->mr_Bytes != mp->mp_End) {
|
|
||||||
hbytes += mn->mr_Bytes;
|
|
||||||
++fcount;
|
|
||||||
}
|
}
|
||||||
if (mn->mr_Next)
|
|
||||||
abytes += (char *)mn->mr_Next - ((char *)mn + mn->mr_Bytes);
|
while (mn != NULL) {
|
||||||
mn = mn->mr_Next;
|
if ((char *)mn + mn->mr_Bytes != mp->mp_End) {
|
||||||
}
|
hbytes += mn->mr_Bytes;
|
||||||
printf(" %d bytes allocated\n%d fragments (%d bytes fragmented)\n",
|
++fcount;
|
||||||
abytes,
|
}
|
||||||
fcount,
|
if (mn->mr_Next != NULL) {
|
||||||
hbytes
|
abytes += (char *)mn->mr_Next -
|
||||||
);
|
((char *)mn + mn->mr_Bytes);
|
||||||
|
}
|
||||||
|
mn = mn->mr_Next;
|
||||||
|
}
|
||||||
|
printf(" %d bytes allocated\n%d fragments (%d bytes fragmented)\n",
|
||||||
|
abytes, fcount, hbytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* This module derived from code donated to the FreeBSD Project by
|
* This module derived from code donated to the FreeBSD Project by
|
||||||
* Matthew Dillon <dillon@backplane.com>
|
* Matthew Dillon <dillon@backplane.com>
|
||||||
*
|
*
|
||||||
* Copyright (c) 1998 The FreeBSD Project
|
* Copyright (c) 1998 The FreeBSD Project
|
||||||
@ -33,23 +33,26 @@
|
|||||||
* DEFS.H
|
* DEFS.H
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define USEGUARD /* use stard/end guard bytes */
|
#ifndef _ZALLOC_DEFS_H
|
||||||
#define USEENDGUARD
|
#define _ZALLOC_DEFS_H
|
||||||
#define DMALLOCDEBUG /* add debugging code to gather stats */
|
|
||||||
#define ZALLOCDEBUG
|
#define USEGUARD /* use stard/end guard bytes */
|
||||||
|
#define USEENDGUARD
|
||||||
|
#define DMALLOCDEBUG /* add debugging code to gather stats */
|
||||||
|
#define ZALLOCDEBUG
|
||||||
|
|
||||||
#include <sys/stdint.h>
|
#include <sys/stdint.h>
|
||||||
#include "stand.h"
|
#include "stand.h"
|
||||||
#include "zalloc_mem.h"
|
#include "zalloc_mem.h"
|
||||||
|
|
||||||
#define Library extern
|
#define Library extern
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* block extension for sbrk()
|
* block extension for sbrk()
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define BLKEXTEND (4 * 1024)
|
#define BLKEXTEND (4 * 1024)
|
||||||
#define BLKEXTENDMASK (BLKEXTEND - 1)
|
#define BLKEXTENDMASK (BLKEXTEND - 1)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Required malloc alignment.
|
* Required malloc alignment.
|
||||||
@ -68,11 +71,13 @@
|
|||||||
#define MALLOCALIGN_MASK (MALLOCALIGN - 1)
|
#define MALLOCALIGN_MASK (MALLOCALIGN - 1)
|
||||||
|
|
||||||
typedef struct Guard {
|
typedef struct Guard {
|
||||||
size_t ga_Bytes;
|
size_t ga_Bytes;
|
||||||
size_t ga_Magic; /* must be at least 32 bits */
|
size_t ga_Magic; /* must be at least 32 bits */
|
||||||
} Guard;
|
} Guard;
|
||||||
|
|
||||||
#define GAMAGIC 0x55FF44FD
|
#define GAMAGIC 0x55FF44FD
|
||||||
#define GAFREE 0x5F54F4DF
|
#define GAFREE 0x5F54F4DF
|
||||||
|
|
||||||
#include "zalloc_protos.h"
|
#include "zalloc_protos.h"
|
||||||
|
|
||||||
|
#endif /* _ZALLOC_DEFS_H */
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* This module derived from code donated to the FreeBSD Project by
|
* This module derived from code donated to the FreeBSD Project by
|
||||||
* Matthew Dillon <dillon@backplane.com>
|
* Matthew Dillon <dillon@backplane.com>
|
||||||
*
|
*
|
||||||
* Copyright (c) 1998 The FreeBSD Project
|
* Copyright (c) 1998 The FreeBSD Project
|
||||||
@ -53,141 +53,145 @@ void mallocstats(void);
|
|||||||
void *
|
void *
|
||||||
Malloc(size_t bytes, const char *file, int line)
|
Malloc(size_t bytes, const char *file, int line)
|
||||||
{
|
{
|
||||||
Guard *res;
|
Guard *res;
|
||||||
|
|
||||||
if (bytes == 0)
|
if (bytes == 0)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
|
||||||
#ifdef USEENDGUARD
|
#ifdef USEENDGUARD
|
||||||
bytes += MALLOCALIGN + 1;
|
bytes += MALLOCALIGN + 1;
|
||||||
#else
|
#else
|
||||||
bytes += MALLOCALIGN;
|
bytes += MALLOCALIGN;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
while ((res = znalloc(&MallocPool, bytes)) == NULL) {
|
while ((res = znalloc(&MallocPool, bytes)) == NULL) {
|
||||||
int incr = (bytes + BLKEXTENDMASK) & ~BLKEXTENDMASK;
|
int incr = (bytes + BLKEXTENDMASK) & ~BLKEXTENDMASK;
|
||||||
char *base;
|
char *base;
|
||||||
|
|
||||||
if ((base = sbrk(incr)) == (char *)-1)
|
if ((base = sbrk(incr)) == (char *)-1)
|
||||||
return(NULL);
|
return (NULL);
|
||||||
zextendPool(&MallocPool, base, incr);
|
zextendPool(&MallocPool, base, incr);
|
||||||
zfree(&MallocPool, base, incr);
|
zfree(&MallocPool, base, incr);
|
||||||
}
|
}
|
||||||
#ifdef DMALLOCDEBUG
|
#ifdef DMALLOCDEBUG
|
||||||
if (++MallocCount > MallocMax)
|
if (++MallocCount > MallocMax)
|
||||||
MallocMax = MallocCount;
|
MallocMax = MallocCount;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USEGUARD
|
#ifdef USEGUARD
|
||||||
res->ga_Magic = GAMAGIC;
|
res->ga_Magic = GAMAGIC;
|
||||||
#endif
|
#endif
|
||||||
res->ga_Bytes = bytes;
|
res->ga_Bytes = bytes;
|
||||||
#ifdef USEENDGUARD
|
#ifdef USEENDGUARD
|
||||||
*((signed char *)res + bytes - 1) = -2;
|
*((signed char *)res + bytes - 1) = -2;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return((char *)res + MALLOCALIGN);
|
return ((char *)res + MALLOCALIGN);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Free(void *ptr, const char *file, int line)
|
Free(void *ptr, const char *file, int line)
|
||||||
{
|
{
|
||||||
size_t bytes;
|
size_t bytes;
|
||||||
|
|
||||||
if (ptr != NULL) {
|
if (ptr != NULL) {
|
||||||
Guard *res = (void *)((char *)ptr - MALLOCALIGN);
|
Guard *res = (void *)((char *)ptr - MALLOCALIGN);
|
||||||
|
|
||||||
if (file == NULL)
|
if (file == NULL)
|
||||||
file = "unknown";
|
file = "unknown";
|
||||||
#ifdef USEGUARD
|
#ifdef USEGUARD
|
||||||
if (res->ga_Magic == GAFREE) {
|
if (res->ga_Magic == GAFREE) {
|
||||||
printf("free: duplicate free @ %p from %s:%d\n", ptr, file, line);
|
printf("free: duplicate free @ %p from %s:%d\n",
|
||||||
return;
|
ptr, file, line);
|
||||||
}
|
return;
|
||||||
if (res->ga_Magic != GAMAGIC)
|
}
|
||||||
panic("free: guard1 fail @ %p from %s:%d", ptr, file, line);
|
if (res->ga_Magic != GAMAGIC)
|
||||||
res->ga_Magic = GAFREE;
|
panic("free: guard1 fail @ %p from %s:%d",
|
||||||
|
ptr, file, line);
|
||||||
|
res->ga_Magic = GAFREE;
|
||||||
#endif
|
#endif
|
||||||
#ifdef USEENDGUARD
|
#ifdef USEENDGUARD
|
||||||
if (*((signed char *)res + res->ga_Bytes - 1) == -1) {
|
if (*((signed char *)res + res->ga_Bytes - 1) == -1) {
|
||||||
printf("free: duplicate2 free @ %p from %s:%d\n", ptr, file, line);
|
printf("free: duplicate2 free @ %p from %s:%d\n",
|
||||||
return;
|
ptr, file, line);
|
||||||
}
|
return;
|
||||||
if (*((signed char *)res + res->ga_Bytes - 1) != -2)
|
}
|
||||||
panic("free: guard2 fail @ %p + %zu from %s:%d", ptr, res->ga_Bytes - MALLOCALIGN, file, line);
|
if (*((signed char *)res + res->ga_Bytes - 1) != -2)
|
||||||
*((signed char *)res + res->ga_Bytes - 1) = -1;
|
panic("free: guard2 fail @ %p + %zu from %s:%d",
|
||||||
|
ptr, res->ga_Bytes - MALLOCALIGN, file, line);
|
||||||
|
*((signed char *)res + res->ga_Bytes - 1) = -1;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bytes = res->ga_Bytes;
|
bytes = res->ga_Bytes;
|
||||||
zfree(&MallocPool, res, bytes);
|
zfree(&MallocPool, res, bytes);
|
||||||
#ifdef DMALLOCDEBUG
|
#ifdef DMALLOCDEBUG
|
||||||
--MallocCount;
|
--MallocCount;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void *
|
void *
|
||||||
Calloc(size_t n1, size_t n2, const char *file, int line)
|
Calloc(size_t n1, size_t n2, const char *file, int line)
|
||||||
{
|
{
|
||||||
uintptr_t bytes = (uintptr_t)n1 * (uintptr_t)n2;
|
uintptr_t bytes = (uintptr_t)n1 * (uintptr_t)n2;
|
||||||
void *res;
|
void *res;
|
||||||
|
|
||||||
if ((res = Malloc(bytes, file, line)) != NULL) {
|
if ((res = Malloc(bytes, file, line)) != NULL) {
|
||||||
bzero(res, bytes);
|
bzero(res, bytes);
|
||||||
#ifdef DMALLOCDEBUG
|
#ifdef DMALLOCDEBUG
|
||||||
if (++MallocCount > MallocMax)
|
if (++MallocCount > MallocMax)
|
||||||
MallocMax = MallocCount;
|
MallocMax = MallocCount;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return(res);
|
return (res);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* realloc() - I could be fancier here and free the old buffer before
|
* realloc() - I could be fancier here and free the old buffer before
|
||||||
* allocating the new one (saving potential fragmentation
|
* allocating the new one (saving potential fragmentation
|
||||||
* and potential buffer copies). But I don't bother.
|
* and potential buffer copies). But I don't bother.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void *
|
void *
|
||||||
Realloc(void *ptr, size_t size, const char *file, int line)
|
Realloc(void *ptr, size_t size, const char *file, int line)
|
||||||
{
|
{
|
||||||
void *res;
|
void *res;
|
||||||
size_t old;
|
size_t old;
|
||||||
|
|
||||||
if ((res = Malloc(size, file, line)) != NULL) {
|
if ((res = Malloc(size, file, line)) != NULL) {
|
||||||
if (ptr) {
|
if (ptr != NULL) {
|
||||||
Guard *g = (Guard *)((char *)ptr - MALLOCALIGN);
|
Guard *g = (Guard *)((char *)ptr - MALLOCALIGN);
|
||||||
|
|
||||||
old = g->ga_Bytes - MALLOCALIGN;
|
old = g->ga_Bytes - MALLOCALIGN;
|
||||||
if (old < size)
|
if (old < size)
|
||||||
bcopy(ptr, res, old);
|
bcopy(ptr, res, old);
|
||||||
else
|
else
|
||||||
bcopy(ptr, res, size);
|
bcopy(ptr, res, size);
|
||||||
Free(ptr, file, line);
|
Free(ptr, file, line);
|
||||||
} else {
|
} else {
|
||||||
#ifdef DMALLOCDEBUG
|
#ifdef DMALLOCDEBUG
|
||||||
if (++MallocCount > MallocMax)
|
if (++MallocCount > MallocMax)
|
||||||
MallocMax = MallocCount;
|
MallocMax = MallocCount;
|
||||||
#ifdef EXITSTATS
|
#ifdef EXITSTATS
|
||||||
if (DidAtExit == 0) {
|
if (DidAtExit == 0) {
|
||||||
DidAtExit = 1;
|
DidAtExit = 1;
|
||||||
atexit(mallocstats);
|
atexit(mallocstats);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
return (res);
|
||||||
return(res);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void *
|
void *
|
||||||
Reallocf(void *ptr, size_t size, const char *file, int line)
|
Reallocf(void *ptr, size_t size, const char *file, int line)
|
||||||
{
|
{
|
||||||
void *res;
|
void *res;
|
||||||
|
|
||||||
if ((res = Realloc(ptr, size, file, line)) == NULL)
|
if ((res = Realloc(ptr, size, file, line)) == NULL)
|
||||||
Free(ptr, file, line);
|
Free(ptr, file, line);
|
||||||
return(res);
|
return (res);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DMALLOCDEBUG
|
#ifdef DMALLOCDEBUG
|
||||||
@ -195,11 +199,10 @@ Reallocf(void *ptr, size_t size, const char *file, int line)
|
|||||||
void
|
void
|
||||||
mallocstats(void)
|
mallocstats(void)
|
||||||
{
|
{
|
||||||
printf("Active Allocations: %d/%d\n", MallocCount, MallocMax);
|
printf("Active Allocations: %d/%d\n", MallocCount, MallocMax);
|
||||||
#ifdef ZALLOCDEBUG
|
#ifdef ZALLOCDEBUG
|
||||||
zallocstats(&MallocPool);
|
zallocstats(&MallocPool);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* This module derived from code donated to the FreeBSD Project by
|
* This module derived from code donated to the FreeBSD Project by
|
||||||
* Matthew Dillon <dillon@backplane.com>
|
* Matthew Dillon <dillon@backplane.com>
|
||||||
*
|
*
|
||||||
* Copyright (c) 1998 The FreeBSD Project
|
* Copyright (c) 1998 The FreeBSD Project
|
||||||
@ -34,20 +34,23 @@
|
|||||||
*
|
*
|
||||||
* Basic memory pool / memory node structures.
|
* Basic memory pool / memory node structures.
|
||||||
*/
|
*/
|
||||||
|
#ifndef _ZALLOC_MEM_H
|
||||||
|
#define _ZALLOC_MEM_H
|
||||||
|
|
||||||
typedef struct MemNode {
|
typedef struct MemNode {
|
||||||
struct MemNode *mr_Next;
|
struct MemNode *mr_Next;
|
||||||
uintptr_t mr_Bytes;
|
uintptr_t mr_Bytes;
|
||||||
} MemNode;
|
} MemNode;
|
||||||
|
|
||||||
typedef struct MemPool {
|
typedef struct MemPool {
|
||||||
void *mp_Base;
|
void *mp_Base;
|
||||||
void *mp_End;
|
void *mp_End;
|
||||||
MemNode *mp_First;
|
MemNode *mp_First;
|
||||||
uintptr_t mp_Size;
|
uintptr_t mp_Size;
|
||||||
uintptr_t mp_Used;
|
uintptr_t mp_Used;
|
||||||
} MemPool;
|
} MemPool;
|
||||||
|
|
||||||
#define ZNOTE_FREE 0
|
#define ZNOTE_FREE 0
|
||||||
#define ZNOTE_REUSE 1
|
#define ZNOTE_REUSE 1
|
||||||
|
|
||||||
|
#endif /* _ZALLOC_MEM_H */
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* This module derived from code donated to the FreeBSD Project by
|
* This module derived from code donated to the FreeBSD Project by
|
||||||
* Matthew Dillon <dillon@backplane.com>
|
* Matthew Dillon <dillon@backplane.com>
|
||||||
*
|
*
|
||||||
* Copyright (c) 1998 The FreeBSD Project
|
* Copyright (c) 1998 The FreeBSD Project
|
||||||
@ -29,7 +29,12 @@
|
|||||||
* $FreeBSD$
|
* $FreeBSD$
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifndef _ZALLOC_PROTOS_H
|
||||||
|
#define _ZALLOC_PROTOS_H
|
||||||
|
|
||||||
Library void *znalloc(struct MemPool *mpool, uintptr_t bytes);
|
Library void *znalloc(struct MemPool *mpool, uintptr_t bytes);
|
||||||
Library void zfree(struct MemPool *mpool, void *ptr, uintptr_t bytes);
|
Library void zfree(struct MemPool *mpool, void *ptr, uintptr_t bytes);
|
||||||
Library void zextendPool(MemPool *mp, void *base, uintptr_t bytes);
|
Library void zextendPool(MemPool *mp, void *base, uintptr_t bytes);
|
||||||
Library void zallocstats(struct MemPool *mp);
|
Library void zallocstats(struct MemPool *mp);
|
||||||
|
|
||||||
|
#endif /* _ZALLOC_PROTOS_H */
|
||||||
|
Loading…
Reference in New Issue
Block a user