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:
Toomas Soome 2019-09-17 11:35:53 +00:00
parent 144c4ca039
commit e57c0c2afb
5 changed files with 270 additions and 260 deletions

View File

@ -88,6 +88,9 @@ typedef char assert_align[(sizeof(struct MemNode) <= MALLOCALIGN) ? 1 : -1];
void *
znalloc(MemPool *mp, uintptr_t bytes)
{
MemNode **pmn;
MemNode *mn;
/*
* align according to pool object size (can be 0). This is
* inclusive of the MEMNODE_SIZE_MASK minimum alignment.
@ -96,18 +99,19 @@ znalloc(MemPool *mp, uintptr_t bytes)
bytes = (bytes + MEMNODE_SIZE_MASK) & ~MEMNODE_SIZE_MASK;
if (bytes == 0)
return((void *)-1);
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 *mn;
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;
for (pmn = &mp->mp_First; (mn=*pmn) != NULL; pmn = &mn->mr_Next) {
if (bytes > mn->mr_Bytes)
continue;
@ -115,10 +119,6 @@ znalloc(MemPool *mp, uintptr_t bytes)
* Cut a chunk of memory out of the beginning of this
* block and fixup the link appropriately.
*/
{
char *ptr = (char *)mn;
if (mn->mr_Bytes == bytes) {
*pmn = mn->mr_Next;
} else {
@ -130,14 +130,12 @@ znalloc(MemPool *mp, uintptr_t bytes)
mp->mp_Used += bytes;
return(ptr);
}
}
}
/*
* Memory pool is full, return NULL.
*/
return(NULL);
return (NULL);
}
/*
@ -147,6 +145,9 @@ znalloc(MemPool *mp, uintptr_t bytes)
void
zfree(MemPool *mp, void *ptr, uintptr_t bytes)
{
MemNode **pmn;
MemNode *mn;
/*
* align according to pool object size (can be 0). This is
* inclusive of the MEMNODE_SIZE_MASK minimum alignment.
@ -168,11 +169,6 @@ zfree(MemPool *mp, void *ptr, uintptr_t bytes)
/*
* free the segment
*/
{
MemNode **pmn;
MemNode *mn;
mp->mp_Used -= bytes;
for (pmn = &mp->mp_First; (mn = *pmn) != NULL; pmn = &mn->mr_Next) {
@ -197,10 +193,11 @@ zfree(MemPool *mp, void *ptr, uintptr_t bytes)
if ((char *)ptr + bytes == (char *)mn) {
((MemNode *)ptr)->mr_Next = mn->mr_Next;
((MemNode *)ptr)->mr_Bytes= bytes + mn->mr_Bytes;
((MemNode *)ptr)->mr_Bytes =
bytes + mn->mr_Bytes;
} else {
((MemNode *)ptr)->mr_Next = mn;
((MemNode *)ptr)->mr_Bytes= bytes;
((MemNode *)ptr)->mr_Bytes = bytes;
}
*pmn = mn = (MemNode *)ptr;
@ -210,14 +207,15 @@ zfree(MemPool *mp, void *ptr, uintptr_t bytes)
*/
if (pmn != &mp->mp_First) {
if ((char*)pmn + ((MemNode*)pmn)->mr_Bytes == (char*)ptr) {
if ((char *)pmn + ((MemNode*)pmn)->mr_Bytes ==
(char *)ptr) {
((MemNode *)pmn)->mr_Next = mn->mr_Next;
((MemNode *)pmn)->mr_Bytes += mn->mr_Bytes;
((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,
@ -229,8 +227,7 @@ zfree(MemPool *mp, void *ptr, uintptr_t bytes)
* previous area if possible.
*/
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_Bytes = bytes;
*pmn = (MemNode *)ptr;
@ -239,7 +236,6 @@ zfree(MemPool *mp, void *ptr, uintptr_t bytes)
((MemNode *)pmn)->mr_Bytes += bytes;
mn = (MemNode *)pmn;
}
}
}
/*
@ -288,7 +284,7 @@ zallocstats(MemPool *mp)
int fcount = 0;
MemNode *mn;
printf("%d bytes reserved", (int) mp->mp_Size);
printf("%d bytes reserved", (int)mp->mp_Size);
mn = mp->mp_First;
@ -296,21 +292,19 @@ zallocstats(MemPool *mp)
abytes += (char *)mn - (char *)mp->mp_Base;
}
while (mn) {
while (mn != NULL) {
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);
if (mn->mr_Next != NULL) {
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
);
abytes, fcount, hbytes);
}
#endif

View File

@ -33,6 +33,9 @@
* DEFS.H
*/
#ifndef _ZALLOC_DEFS_H
#define _ZALLOC_DEFS_H
#define USEGUARD /* use stard/end guard bytes */
#define USEENDGUARD
#define DMALLOCDEBUG /* add debugging code to gather stats */
@ -76,3 +79,5 @@ typedef struct Guard {
#define GAFREE 0x5F54F4DF
#include "zalloc_protos.h"
#endif /* _ZALLOC_DEFS_H */

View File

@ -69,7 +69,7 @@ Malloc(size_t bytes, const char *file, int line)
char *base;
if ((base = sbrk(incr)) == (char *)-1)
return(NULL);
return (NULL);
zextendPool(&MallocPool, base, incr);
zfree(&MallocPool, base, incr);
}
@ -85,7 +85,7 @@ Malloc(size_t bytes, const char *file, int line)
*((signed char *)res + bytes - 1) = -2;
#endif
return((char *)res + MALLOCALIGN);
return ((char *)res + MALLOCALIGN);
}
void
@ -100,20 +100,24 @@ Free(void *ptr, const char *file, int line)
file = "unknown";
#ifdef USEGUARD
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",
ptr, file, line);
return;
}
if (res->ga_Magic != GAMAGIC)
panic("free: guard1 fail @ %p from %s:%d", ptr, file, line);
panic("free: guard1 fail @ %p from %s:%d",
ptr, file, line);
res->ga_Magic = GAFREE;
#endif
#ifdef USEENDGUARD
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",
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);
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
@ -139,7 +143,7 @@ Calloc(size_t n1, size_t n2, const char *file, int line)
MallocMax = MallocCount;
#endif
}
return(res);
return (res);
}
/*
@ -155,7 +159,7 @@ Realloc(void *ptr, size_t size, const char *file, int line)
size_t old;
if ((res = Malloc(size, file, line)) != NULL) {
if (ptr) {
if (ptr != NULL) {
Guard *g = (Guard *)((char *)ptr - MALLOCALIGN);
old = g->ga_Bytes - MALLOCALIGN;
@ -177,7 +181,7 @@ Realloc(void *ptr, size_t size, const char *file, int line)
#endif
}
}
return(res);
return (res);
}
void *
@ -187,7 +191,7 @@ Reallocf(void *ptr, size_t size, const char *file, int line)
if ((res = Realloc(ptr, size, file, line)) == NULL)
Free(ptr, file, line);
return(res);
return (res);
}
#ifdef DMALLOCDEBUG
@ -202,4 +206,3 @@ mallocstats(void)
}
#endif

View File

@ -34,6 +34,8 @@
*
* Basic memory pool / memory node structures.
*/
#ifndef _ZALLOC_MEM_H
#define _ZALLOC_MEM_H
typedef struct MemNode {
struct MemNode *mr_Next;
@ -51,3 +53,4 @@ typedef struct MemPool {
#define ZNOTE_FREE 0
#define ZNOTE_REUSE 1
#endif /* _ZALLOC_MEM_H */

View File

@ -29,7 +29,12 @@
* $FreeBSD$
*/
#ifndef _ZALLOC_PROTOS_H
#define _ZALLOC_PROTOS_H
Library void *znalloc(struct MemPool *mpool, 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 zallocstats(struct MemPool *mp);
#endif /* _ZALLOC_PROTOS_H */