parent
e231246400
commit
53d65a235a
@ -1,8 +1,16 @@ |
||||
#ifndef DEVICE_H |
||||
#define DEVICE_H |
||||
|
||||
#include <stdlib.h> |
||||
#include "utils/array.h" |
||||
|
||||
typedef struct DEVICE device_t; |
||||
|
||||
// Device layer
|
||||
void device_init(); |
||||
array_t* device_get_array(); |
||||
|
||||
// Handling a single device
|
||||
char* device_get_image(device_t* device); |
||||
char* device_get_name(device_t* device); |
||||
|
||||
|
@ -0,0 +1,74 @@ |
||||
#include "array.h" |
||||
#include <stdio.h> |
||||
|
||||
#define DEFAULT_ARRAY_SIZE 16 |
||||
|
||||
struct ARRAY |
||||
{ |
||||
void** data; |
||||
size_t count; |
||||
size_t size; |
||||
}; |
||||
|
||||
array_t* array_alloc() |
||||
{ |
||||
array_t* tr = malloc(sizeof(array_t)); |
||||
tr->data = malloc(sizeof(void*) * DEFAULT_ARRAY_SIZE); |
||||
tr->size = DEFAULT_ARRAY_SIZE; |
||||
tr->count = 0; |
||||
|
||||
return tr; |
||||
} |
||||
|
||||
void array_free(array_t* array) |
||||
{ |
||||
free(array->data); |
||||
free(array); |
||||
} |
||||
|
||||
void array_put(array_t* array, size_t index, void* elem) |
||||
{ |
||||
// Make sure allocated space is big enough
|
||||
if(index >= array->size) |
||||
{ |
||||
array->data = realloc(array->data, (index + 1) * 2); |
||||
if(!array->data) |
||||
{ |
||||
perror("Catastrophic memory allocation failure:"); |
||||
exit(EXIT_FAILURE); |
||||
} |
||||
array->size = (index + 1) * 2; |
||||
} |
||||
|
||||
// Put element in array
|
||||
array->data[index] = elem; |
||||
|
||||
// Update array count (max element position) if necessary
|
||||
if(array->count <= index) array->count = index + 1; |
||||
} |
||||
|
||||
void array_add(array_t* array, void* elem) |
||||
{ |
||||
array_put(array, array->count, elem); |
||||
} |
||||
|
||||
void* array_get(array_t* array, size_t index) |
||||
{ |
||||
if(index >= array->size) |
||||
{ |
||||
fprintf(stderr, "Catastrophic memory access failure: Trying to access index %lu of %lu-sized array\n", index, array->size); |
||||
exit(EXIT_FAILURE); |
||||
} |
||||
|
||||
if(index >= array->count) |
||||
{ |
||||
fprintf(stderr, "Warning: Trying to access index %lu of array with %lu element, uninitialized access\n", index, array->count); |
||||
} |
||||
|
||||
return array->data[index]; |
||||
} |
||||
|
||||
size_t array_count(array_t* array) |
||||
{ |
||||
return array->count; |
||||
} |
@ -0,0 +1,17 @@ |
||||
#ifndef ARRAY_H |
||||
#define ARRAY_H |
||||
|
||||
#include <stdlib.h> |
||||
|
||||
typedef struct ARRAY array_t; |
||||
|
||||
array_t* array_alloc(); |
||||
void array_free(array_t* array); |
||||
|
||||
void array_add(array_t* array, void* elem); |
||||
void array_put(array_t* array, size_t index, void* elem); |
||||
|
||||
void* array_get(array_t* array, size_t index); |
||||
size_t array_count(array_t* array); |
||||
|
||||
#endif |
Loading…
Reference in new issue