From 11e9dec049e64989c5f3656415d00c6c69851e8e Mon Sep 17 00:00:00 2001 From: cbdev Date: Sun, 4 Jun 2017 18:13:29 +0200 Subject: Move instance allocator to backend object, introduce backend start function --- backend.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) (limited to 'backend.c') diff --git a/backend.c b/backend.c index 673fe9b..1d728d1 100644 --- a/backend.c +++ b/backend.c @@ -4,6 +4,66 @@ static size_t nbackends = 0; static backend* backends = NULL; +static size_t ninstances = 0; +static instance** instances = NULL; + +instance* mm_instance(){ + instance** new_inst = realloc(instances, (ninstances + 1) * sizeof(instance*)); + if(!new_inst){ + //TODO free + fprintf(stderr, "Failed to allocate memory\n"); + ninstances = 0; + return NULL; + } + instances = new_inst; + instances[ninstances] = calloc(1, sizeof(instance)); + if(!instances[ninstances]){ + fprintf(stderr, "Failed to allocate memory\n"); + return NULL; + } + + return instances[ninstances++]; +} + +int mm_backend_instances(char* name, size_t* ninst, instance*** inst){ + backend* b = backend_match(name); + size_t n = 0, u; + //count number of affected instances + for(u = 0; u < ninstances; u++){ + if(instances[u]->backend == b){ + n++; + } + } + + *ninst = n; + *inst = calloc(n, sizeof(instance*)); + if(!*inst){ + fprintf(stderr, "Failed to allocate memory\n"); + return 1; + } + + n = 0; + for(u = 0; u < ninstances; u++){ + if(instances[u]->backend == b){ + (*inst)[n] = instances[u]; + n++; + } + } + return 0; +} + +void instances_free(){ + size_t u; + for(u = 0; u < ninstances; u++){ + free(instances[u]->name); + instances[u]->name = NULL; + instances[u]->backend = NULL; + free(instances[u]); + instances[u] = NULL; + } + free(instances); + ninstances = 0; +} backend* backend_match(char* name){ size_t u; @@ -15,6 +75,16 @@ backend* backend_match(char* name){ return NULL; } +instance* instance_match(char* name){ + size_t u; + for(u = 0; u < ninstances; u++){ + if(!strcmp(instances[u]->name, name)){ + return instances[u]; + } + } + return NULL; +} + int mm_backend_register(backend b){ if(!backend_match(b.name)){ backends = realloc(backends, (nbackends + 1) * sizeof(backend)); @@ -32,6 +102,15 @@ int mm_backend_register(backend b){ return 1; } +int backends_start(){ + int rv = 0; + size_t u; + for(u = 0; u < nbackends; u++){ + rv |= backends[u].start(); + } + return rv; +} + int backends_stop(){ size_t u; for(u = 0; u < nbackends; u++){ -- cgit v1.2.3