slot.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Public Domain
  2. #include "slot.h"
  3. void slot_resize(struct slot_base_props* base, size_t chunk_mem_size, size_t chunk_len) {
  4. if(base->chunksLen >= base->chunksAlloc) {
  5. if(base->chunksAlloc == 0) base->chunksAlloc = 4;
  6. else base->chunksAlloc *= 2;
  7. base->data = realloc(base->data, base->chunksAlloc * sizeof(void*));
  8. }
  9. base->data[base->chunksLen++] = calloc(1, chunk_mem_size);
  10. base->alloc += chunk_len;
  11. }
  12. // returns 1 to continue, 0 top stop
  13. int slot_next(struct slot_base_props* base, uint64_t chunkLen, uint64_t* c, uint64_t* i, int inc) {
  14. if(inc) goto INC;
  15. while(1) {
  16. uint64_t* occ = base->data[*c];
  17. if(occ[*i / 64] & (1ul << (*i % 64))) {
  18. // found
  19. return 1;
  20. }
  21. INC:
  22. (*i)++;
  23. if(*i >= chunkLen) {
  24. *i = 0;
  25. (*c)++;
  26. }
  27. if(*c >= base->chunksLen) {
  28. // end of array;
  29. return 0;
  30. }
  31. }
  32. return 0;
  33. }
  34. void slot_free(struct slot_base_props* base) {
  35. for(long i = 0; i < base->chunksLen; i++) {
  36. free(base->data[i]);
  37. }
  38. free(base->data);
  39. base->chunksAlloc = 0;
  40. base->chunksLen = 0;
  41. base->fill = 0;
  42. base->alloc = 0;
  43. base->nextFree = 0;
  44. }