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.
@ -103,11 +106,12 @@ znalloc(MemPool *mp, uintptr_t bytes)
* 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;
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,8 +130,6 @@ znalloc(MemPool *mp, uintptr_t bytes)
mp->mp_Used += bytes;
return(ptr);
}
}
}
/*
* Memory pool is full, 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,7 +193,8 @@ 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;
@ -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;
@ -240,7 +237,6 @@ zfree(MemPool *mp, void *ptr, uintptr_t bytes)
mn = (MemNode *)pmn;
}
}
}
/*
* zextendPool() - extend memory pool to cover additional space.
@ -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

@ -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
@ -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;
@ -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 */