new.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. ** Written by David Gerdes US Army Construction Engineering Research Lab
  3. ** April 1992
  4. ** Copyright 1992 USA-CERL All rights reserved.
  5. **
  6. */
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <grass/linkm.h>
  10. VOID_T *link_new(struct link_head *Head)
  11. {
  12. VOID_T *tmp;
  13. char *ctmp, *p;
  14. register int i;
  15. if (Head->Unused == NULL) {
  16. if (Head->max_ptr >= Head->alloced) {
  17. /*DEBUG fprintf (stderr, "REALLOCING PTR_ARRAY (%d -> %d)\n", Head->alloced, Head->alloced * 2); */
  18. if (NULL ==
  19. (tmp =
  20. (VOID_T *) realloc(Head->ptr_array,
  21. sizeof(VOID_T *) * Head->alloced * 2))) {
  22. if (Head->exit_flag)
  23. link_out_of_memory();
  24. return NULL;
  25. }
  26. Head->ptr_array = (VOID_T **) tmp;
  27. Head->alloced *= 2;
  28. }
  29. /*DEBUG fprintf (stderr, "Mallocing another chunk: %d\n", Head->max_ptr); */
  30. if (NULL == (tmp = (VOID_T *)
  31. malloc(Head->chunk_size * Head->unit_size))) {
  32. if (Head->exit_flag)
  33. link_out_of_memory();
  34. return NULL;
  35. }
  36. Head->ptr_array[Head->max_ptr++] = (VOID_T *) tmp;
  37. Head->Unused = (VOID_T *) tmp;
  38. p = ctmp = (char *)tmp;
  39. for (i = 0; i < Head->chunk_size - 1; i++) {
  40. link__set_next((VOID_T *) p,
  41. (VOID_T *) & (ctmp[(i + 1) * Head->unit_size]));
  42. /* p->next = p+1 */
  43. p = &(ctmp[(i + 1) * Head->unit_size]); /* p = p->next */
  44. }
  45. link__set_next((VOID_T *) p, NULL); /* p->next = NULL */
  46. }
  47. tmp = Head->Unused;
  48. /* Unused = Unused->next */
  49. Head->Unused = link__get_next(Head->Unused);
  50. return tmp;
  51. }