diff --git a/configure b/configure index e5c2e35..356c3f5 100644 --- a/configure +++ b/configure @@ -55,6 +55,7 @@ HDRS=' udp.h agetaddrinfo.h apoll.h + mem.h ' # List of .c files @@ -79,6 +80,7 @@ SRCS=' udp.c agetaddrinfo.c apoll.c + mem.c ' if make -v 2>&1 | grep GNU > /dev/null 2>&1 ; then diff --git a/main_amiga.c b/main_amiga.c index 4ddd8f3..cf63ae0 100644 --- a/main_amiga.c +++ b/main_amiga.c @@ -69,9 +69,10 @@ #include "tz.h" #include "ntimed.h" +#include "ntimed_platform.h" +#include "mem.h" #include "ntp.h" #include "udp.h" -#include "ntimed_platform.h" #define PARAM_CLIENT PARAM_INSTANCE #define PARAM_TABLE_NAME client_param_table @@ -279,5 +280,7 @@ static void clean_exit() FreeArgs(rdargs); rdargs = NULL; } + + freeall(); } diff --git a/mem.c b/mem.c new file mode 100644 index 0000000..c1189c7 --- /dev/null +++ b/mem.c @@ -0,0 +1,201 @@ +/* + * Copyright (c) 2015 Carsten Larsen + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include + +#include "ntimed.h" +#include "mem.h" + +struct MemoryBlock +{ + struct MemoryBlock *next; + size_t size; + void *address; +}; + +struct MemoryList +{ + struct MemoryBlock *first; + struct MemoryBlock *last; + long size; + long count; +}; + +struct MemoryList *list = NULL; + +void allocerror(char*, size_t); +void deallocerror(char*, void*); + +void* allocmem(size_t size) +{ + struct MemoryBlock *newblock; + size_t allocsize; + + if (list == NULL) { + list = (struct MemoryList*)AllocVec(sizeof(struct MemoryList), MEMF_ANY | MEMF_CLEAR); + if (!list) { + allocerror("list", sizeof(struct MemoryList)); + return 0; + } + + list->first = NULL; + list->last = NULL; + list->size = 0; + list->count = 0; + } + + // Align to bytes of 4 + allocsize = (size + 3) & ~0x03; + + newblock = (struct MemoryBlock*)AllocVec(sizeof(struct MemoryBlock), MEMF_ANY | MEMF_CLEAR); + if (!newblock) { + allocerror("block", sizeof(struct MemoryBlock)); + return 0; + } + + newblock->address = (struct MemoryBlock*)AllocVec(allocsize, MEMF_ANY | MEMF_CLEAR); + if (!newblock->address) { + FreeVec(newblock); + allocerror("memory", allocsize); + return 0; + } + + newblock->size = allocsize; + newblock->next = NULL; + + if(list->first == NULL) { + list->first = newblock; + list->last = newblock; + } else { + list->last->next = newblock; + list->last = newblock; + } + + list->size += allocsize; + list->count++; + + // DEBUG code + // Debug(NULL, "Mememory allocated at address (%x)\n", newblock->address); + + return newblock->address; +} + +void freemem(void* block) +{ + struct MemoryBlock *current, *last; + + if (list == NULL || block == NULL) { + deallocerror("list", 0); + return; + } + + if (block == NULL) { + deallocerror("memory", 0); + return; + } + + last = NULL; + current = list->first; + while (current != NULL && current->address != block) { + last = current; + current = current->next; + } + + if (current == NULL) { + deallocerror("address not found", block); + return; + } + + list->size -= current->size; + list->count--; + + if (list->first == current) { + list->first = NULL; + list->last = NULL; + } else if (list->last == current) { + last->next = current->next; + list->last = last; + } else { + last->next = current->next; + } + + FreeVec(current->address); + FreeVec(current); + + // DEBUG code + // Debug(NULL, "Mememory deallocated at address (%x)\n", block); +} + +void freeall() +{ + struct MemoryBlock *current, *next; + + if (list == NULL) { + return; + } + + current = list->first; + while (current != NULL) { + next = current->next; + FreeVec(current->address); + FreeVec(current); + current = next; + } + + FreeVec(list); + list = NULL; +} + +void allocerror(char *descr, size_t size) +{ + Debug(NULL, "Mememory allocation error (%s) with size (%d)\n", descr, size); +} + +void deallocerror(char *descr, void *p) +{ + Debug(NULL, "Mememory deallocation error (%s) address (%x)\n", descr, p); +} + +#ifdef __c_plus +inline void* operator new (size_t size) { + return allocmem(size); +} + +inline void* operator new[] (size_t size) { + return allocmem(size); +} + +inline void operator delete (void* ptr) { + freemem(ptr); +} + +inline void operator delete[] (void* ptr) { + freemem(ptr); +} +#endif diff --git a/mem.h b/mem.h new file mode 100644 index 0000000..bbb42b8 --- /dev/null +++ b/mem.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2015 Carsten Larsen + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef AMIGA_MEM_H +#define AMIGA_MEM_H + +#include + +void* allocmem(size_t); +void freemem(void*); +void freeall(); + +#endif diff --git a/ntimed_tricks.h b/ntimed_tricks.h index bd2337e..092df93 100644 --- a/ntimed_tricks.h +++ b/ntimed_tricks.h @@ -30,6 +30,7 @@ #define NTIMED_TRICKS_H #include +#include "mem.h" /********************************************************************** * Assert and friends @@ -133,7 +134,7 @@ #define ALLOC_OBJ(to, type_magic) \ do { \ - (to) = calloc(1L, sizeof *(to)); \ + (to) = allocmem(sizeof *(to)); \ if ((to) != NULL) \ (to)->magic = (type_magic); \ } while (0) @@ -141,7 +142,7 @@ #define FREE_OBJ(to) \ do { \ (to)->magic = (0); \ - free(to); \ + freemem(to); \ } while (0) #define VALID_OBJ(ptr, type_magic) \ diff --git a/ntp_peer.c b/ntp_peer.c index 6a7ae36..ac20b33 100644 --- a/ntp_peer.c +++ b/ntp_peer.c @@ -50,13 +50,17 @@ NTP_Peer_New(const char *hostname, const void *sa, unsigned salen) AN(np); np->sa_len = salen; - np->sa = calloc(1, np->sa_len); + np->sa = allocmem(np->sa_len); AN(np->sa); memcpy(np->sa, sa, np->sa_len); - np->hostname = strdup(hostname); +// np->hostname = strdup(hostname); + np->hostname = allocmem(strlen(hostname) + 1); + memcpy(np->hostname, hostname, strlen(hostname) + 1); AN(np->hostname); - np->ip = strdup(hbuf); +// np->ip = strdup(hbuf); + np->ip = allocmem(strlen(hbuf) + 1); + memcpy(np->ip, hbuf, strlen(hbuf) + 1); AN(np->ip); ALLOC_OBJ(np->tx_pkt, NTP_PACKET_MAGIC); @@ -93,13 +97,12 @@ NTP_Peer_NewLookup(struct ocx *ocx, const char *hostname) void NTP_Peer_Destroy(struct ntp_peer *np) { - CHECK_OBJ_NOTNULL(np, NTP_PEER_MAGIC); - free(np->sa); - free(np->hostname); - free(np->ip); - free(np->tx_pkt); - free(np->rx_pkt); + freemem(np->sa); + freemem(np->hostname); + freemem(np->ip); + freemem(np->tx_pkt); + freemem(np->rx_pkt); FREE_OBJ(np); } diff --git a/ntp_peerset.c b/ntp_peerset.c index 40cbb35..3f98703 100644 --- a/ntp_peerset.c +++ b/ntp_peerset.c @@ -168,7 +168,9 @@ ntp_peerset_add_group(struct ntp_peerset *nps, const char *name) ALLOC_OBJ(ng, NTP_GROUP_MAGIC); AN(ng); - ng->hostname = strdup(name); +// ng->hostname = strdup(name); + ng->hostname = allocmem(strlen(name) + 1); + memcpy(ng->hostname, name, strlen(name) + 1); AN(ng->hostname); TAILQ_INSERT_TAIL(&nps->group, ng, list); nps->ngroup++;