list.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "sam.h"
  2. /*
  3. * Check that list has room for one more element.
  4. */
  5. static void
  6. growlist(List *l, int esize)
  7. {
  8. uchar *p;
  9. if(l->listptr == nil || l->nalloc == 0){
  10. l->nalloc = INCR;
  11. l->listptr = emalloc(INCR*esize);
  12. l->nused = 0;
  13. }
  14. else if(l->nused == l->nalloc){
  15. p = erealloc(l->listptr, (l->nalloc+INCR)*esize);
  16. l->listptr = p;
  17. memset(p+l->nalloc, 0, INCR*esize);
  18. l->nalloc += INCR;
  19. }
  20. }
  21. /*
  22. * Remove the ith element from the list
  23. */
  24. void
  25. dellist(List *l, int i)
  26. {
  27. Posn *pp;
  28. void **vpp;
  29. l->nused--;
  30. switch(l->type){
  31. case 'P':
  32. pp = l->posnptr+i;
  33. memmove(pp, pp+1, (l->nused-i)*sizeof(*pp));
  34. break;
  35. case 'p':
  36. vpp = l->voidpptr+i;
  37. memmove(vpp, vpp+1, (l->nused-i)*sizeof(*vpp));
  38. break;
  39. }
  40. }
  41. /*
  42. * Add a new element, whose position is i, to the list
  43. */
  44. void
  45. inslist(List *l, int i, ...)
  46. {
  47. Posn *pp;
  48. void **vpp;
  49. va_list list;
  50. va_start(list, i);
  51. switch(l->type){
  52. case 'P':
  53. growlist(l, sizeof(*pp));
  54. pp = l->posnptr+i;
  55. memmove(pp+1, pp, (l->nused-i)*sizeof(*pp));
  56. *pp = va_arg(list, Posn);
  57. break;
  58. case 'p':
  59. growlist(l, sizeof(*vpp));
  60. vpp = l->voidpptr+i;
  61. memmove(vpp+1, vpp, (l->nused-i)*sizeof(*vpp));
  62. *vpp = va_arg(list, void*);
  63. break;
  64. }
  65. va_end(list);
  66. l->nused++;
  67. }
  68. void
  69. listfree(List *l)
  70. {
  71. free(l->listptr);
  72. free(l);
  73. }
  74. List*
  75. listalloc(int type)
  76. {
  77. List *l;
  78. l = emalloc(sizeof(List));
  79. l->type = type;
  80. l->nalloc = 0;
  81. l->nused = 0;
  82. return l;
  83. }