From d2b6b96df63ac92e5456bc34f5c71ef464e023c3 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 25 Jan 2019 11:03:15 +0100 Subject: [PATCH] Explicitly use clientID for newOVerTimeClient() instead of using the private variable overTimeClientCount to avoid (possibly) error-prone double-entry bookkeeping. Signed-off-by: DL6ER --- datastructure.c | 7 ++++--- routines.h | 2 +- shmem.c | 20 +++++++++----------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/datastructure.c b/datastructure.c index 18cb871a..a8dab52b 100644 --- a/datastructure.c +++ b/datastructure.c @@ -218,11 +218,12 @@ int findClientID(const char *client) // to be done separately to be non-blocking clients[clientID].new = true; clients[clientID].namepos = 0; - // Increase counter by one - counters->clients++; // Create new overTime client data - newOverTimeClient(); + newOverTimeClient(clientID); + + // Increase counter by one + counters->clients++; return clientID; } diff --git a/routines.h b/routines.h index b4ad082b..49b518f3 100644 --- a/routines.h +++ b/routines.h @@ -119,7 +119,7 @@ void *enlarge_shmem_struct(char type); * Create a new overTime client shared memory block. * This also updates `overTimeClientData`. */ -void newOverTimeClient(); +void newOverTimeClient(int clientID); /** * Add a new overTime slot to each overTime client shared memory block. diff --git a/shmem.c b/shmem.c index dd7b1e6f..afbeb3d0 100644 --- a/shmem.c +++ b/shmem.c @@ -33,7 +33,6 @@ static SharedMemory shm_forwarded = { 0 }; static SharedMemory shm_overTime = { 0 }; static SharedMemory *shm_overTimeClients = NULL; -static int overTimeClientCount = 0; typedef struct { pthread_mutex_t lock; @@ -88,33 +87,32 @@ static char *clientShmName(int id) { return name; } -void newOverTimeClient() { +void newOverTimeClient(int clientID) { // Get the name of the new shared memory. // This will be used in the struct, so it should not be immediately freed. - char *name = clientShmName(overTimeClientCount); + char *name = clientShmName(clientID); // Create the shared memory with enough space for the current overTime slots shm_unlink(name); SharedMemory shm = create_shm(name, (counters->overTime/pagesize + 1)*pagesize*sizeof(int)); if(shm.ptr == NULL) { free(shm.name); - logg("Failed to initialize new overTime client %d", overTimeClientCount); + logg("Failed to initialize new overTime client %d", clientID); return; } // Make space for the new shared memory - shm_overTimeClients = realloc(shm_overTimeClients, sizeof(SharedMemory) * (overTimeClientCount + 1)); - overTimeClientCount++; - shm_overTimeClients[overTimeClientCount-1] = shm; + shm_overTimeClients = realloc(shm_overTimeClients, sizeof(SharedMemory) * (clientID + 1)); + shm_overTimeClients[clientID] = shm; // Add to overTimeClientData - overTimeClientData = realloc(overTimeClientData, sizeof(int*) * (overTimeClientCount)); - overTimeClientData[overTimeClientCount-1] = shm.ptr; + overTimeClientData = realloc(overTimeClientData, sizeof(int*) * (clientID + 1)); + overTimeClientData[clientID] = shm.ptr; } void addOverTimeClientSlot() { // For each client slot, add pagesize overTime slots - for(int i = 0; i < overTimeClientCount; i++) + for(int i = 0; i < counters->clients; i++) { // Only increase the size of the shm object if needed // shm_overTimeClients[i].size stores the size of the memory in bytes whereas @@ -276,7 +274,7 @@ void destroy_shmem(void) delete_shm(&shm_forwarded); delete_shm(&shm_overTime); - for(int i = 0; i < overTimeClientCount; i++) { + for(int i = 0; i < counters->clients; i++) { delete_shm(&shm_overTimeClients[i]); free(shm_overTimeClients[i].name); }