int list_add_item(list *listo, void *item, char *name) { int name_length; char *new_name; if( list_is_full(listo) ) { if( listo->growable ) list_grow(listo); else return -1; } listo->names[listo->item_count] = NULL; if(name != NULL) { name_length = strlen(name); new_name = (char*) malloc(sizeof(char) * name_length + 1); strncpy(new_name, name, name_length); listo->names[listo->item_count] = new_name; } listo->items[listo->item_count] = item; listo->item_count++; return listo->item_count-1; }
/* adds an element to the end of the list */ int mmu_list_append(mmu_list_t *list, const void *base, size_t extent) { mmu_list_item_t *new_item; int rc = MMU_FAILURE; if (NULL == list || NULL == base || 0 == extent) { return MMU_FAILURE_INVALID_ARG; } /* construct the new list element */ if (MMU_SUCCESS != (rc = list_item_construct(&new_item, base, extent))) { goto out; } /* list_grow takes care of all the magic dealing with whether or not the * list will actually grow. so, always call this with size + 1 before we add * the new item to the list. */ if (MMU_SUCCESS != (rc = list_grow(list, list->size + 1))) { goto out; } /* add the item to the list */ list->item_ptrs[list->size] = new_item; list->size++; out: if (MMU_SUCCESS != rc) list_item_destruct(new_item, false); return rc; }
void list_add(list *l, void *e_addr) { list_grow(l); void* target = (char*) l->elements + l->logical_len*l->elem_size; memcpy(target, e_addr, l->elem_size); l->logical_len++; }
int uproc_list_append(uproc_list *list, const void *value) { int res = list_grow(list, 1); if (res) { return res; } list->size++; return uproc_list_set(list, list->size - 1, value); }
void list_add(struct list_t *list, void *elem) { /* Grow list if necessary */ if (list->count == list->size) list_grow(list); /* Add element */ list->elem[list->tail] = elem; list->tail = (list->tail + 1) % list->size; list->count++; list->error_code = LIST_ERR_OK; }
void list_add_at(list *l, int pos, void *e_addr) { list_grow(l); for (int i = l->logical_len; i > pos; --i) { void* next = (char*) l->elements + i*l->elem_size; void* prev = (char*) l->elements + (i - 1)*l->elem_size; memcpy(next, prev, l->elem_size); } void* target = (char*) l->elements + pos*l->elem_size; memcpy(target, e_addr, l->elem_size); l->logical_len++; }
int list_append( list_t *list_in, void *val_in ) { /* If there isn't enough memory allocated */ if( list_in->size + 1 > list_in->alloc ) { if( list_grow( list_in, LIST_SIZE_INC ) == -1 ) return -1; } else /* If there is enough, then either there is some at the beginning or some at the end */ { if( (byte_t*)list_in->start > (byte_t*)list_in->pointer ) if( list_shift( list_in, -1 ) == -1 ) return -1; } bcopy( val_in, (byte_t*) list_in->start + list_in->size * list_in->step, list_in->step ); ++list_in->size; return list_in->size; }
int uproc_list_extend(uproc_list *list, const void *values, long n) { int res; if (n < 0) { return uproc_error_msg(UPROC_EINVAL, "negative number of items"); } if (n > (LONG_MAX - list->size)) { return uproc_error_msg(UPROC_EINVAL, "list too big"); } res = list_grow(list, n); if (res) { return res; } memcpy(list->data + list->size * list->value_size, values, n * list->value_size); list->size += n; return 0; }
int list_prepend( list_t *list_in, void *val_in ) { if( list_in->size + 1 > list_in->alloc ) { if( list_grow( list_in, LIST_SIZE_INC ) == -1 ) return -1; if( list_shift( list_in, 1 ) == -1 ) return -1; } else { if( list_in->start <= list_in->pointer ) /* Meaning there is no memory allocated to the left, so make some */ if( list_shift( list_in, 1 ) == -1 ) return -1; } list_in->start = (byte_t*) list_in->start - list_in->step; bcopy( val_in, list_in->start, list_in->step ); ++list_in->size; return list_in->size; }
void list_insert(struct list_t *list, int index, void *elem) { int shiftcount; int pos; int i; /* Check bounds */ if (index < 0 || index > list->count) { list->error_code = LIST_ERR_BOUNDS; return; } /* Grow list if necessary */ if (list->count == list->size) list_grow(list); /* Choose whether to shift elements on the right increasing 'tail', or * shift elements on the left decreasing 'head'. */ if (index > list->count / 2) { shiftcount = list->count - index; for (i = 0, pos = list->tail; i < shiftcount; i++, pos = INLIST(pos - 1)) list->elem[pos] = list->elem[INLIST(pos - 1)]; list->tail = (list->tail + 1) % list->size; } else { for (i = 0, pos = list->head; i < index; i++, pos = (pos + 1) % list->size) list->elem[INLIST(pos - 1)] = list->elem[pos]; list->head = INLIST(list->head - 1); } list->elem[(list->head + index) % list->size] = elem; list->count++; list->error_code = LIST_ERR_OK; }
int list_insert( list_t *list_in, void *ptr_in, int idx_in ) { if( list_in->size + 1 > list_in->alloc ) { if( list_grow( list_in, LIST_SIZE_INC ) == -1 ) return -1; } if( (byte_t*) list_in->start - (byte_t*) list_in->pointer > 0 ) { if( list_shift_set( list_in, 0, idx_in + 1, -1 ) == -1 ) return -1; bcopy( ptr_in, (byte_t*) list_in->start + idx_in * list_in->step, list_in->step ); /* Remember actually inserting into idx_in + 1 position */ list_in->start = (byte_t*) list_in->start - list_in->step; } else { if( list_shift_set( list_in, idx_in + 1, list_in->size - idx_in - 1, 1 ) == -1 ) return -1; bcopy( ptr_in, (byte_t*) list_in->start + ( idx_in + 1 ) * list_in->step, list_in->step ); } ++list_in->size; return 0; }
/* ////////////////////////////////////////////////////////////////////////// */ int mmu_list_reserve(mmu_list_t *list, size_t reserve_size) { return list_grow(list, reserve_size); }