Explicitly use clientID for newOVerTimeClient() instead of using the private variable overTimeClientCount to avoid (possibly) error-prone double-entry bookkeeping.

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2019-01-25 11:03:15 +01:00
parent ae1d09b065
commit d2b6b96df6
3 changed files with 14 additions and 15 deletions
+4 -3
View File
@@ -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;
}
+1 -1
View File
@@ -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.
+9 -11
View File
@@ -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);
}