#include "xslaves.h" #include #include XSlaveInfo new_slave_info() { XSlaveInfo si; si.name = NULL; si.dev_node = NULL; return si; } void free_slave_info(XSlaveInfo si) { free(si.name); free(si.dev_node); si.name = NULL; si.dev_node = NULL; } XSlaves* new_xslaves(size_t capacity) { XSlaves* s = (XSlaves*)malloc(sizeof(XSlaves)); s->capacity = capacity; s->length = 0; s->slaves = (XSlaveInfo*)malloc(capacity*sizeof(XSlaveInfo)); return s; } void free_xslaves(XSlaves* slaves) { for (int i = 0; i < slaves->length; i++) free_slave_info(slaves->slaves[i]); free(slaves->slaves); slaves->slaves = NULL; slaves->length = 0; slaves->capacity = 0; free(slaves); } int add_slaveinfo(XSlaves* slaves, char* name, char* dev_node) { XSlaveInfo si = new_slave_info(); si.name = strdup(name); si.dev_node = strdup(dev_node); if (si.name == NULL || si.dev_node == NULL) { free(si.name); free(si.dev_node); return MALLOCFAIL; } if (slaves->length < slaves->capacity) { slaves->slaves[slaves->length] = si; slaves->length++; return 0; } return XSLAVESFULL; }