Advertisement
SergeyL

list_t.h

Sep 11th, 2013
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.59 KB | None | 0 0
  1. // Copyright (c) 2013, Sergey lamzin
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are met:
  6. //
  7. // 1. Redistributions of source code must retain the above copyright notice, this
  8. //    list of conditions and the following disclaimer.
  9. // 2. Redistributions in binary form must reproduce the above copyright notice,
  10. //    this list of conditions and the following disclaimer in the documentation
  11. //    and/or other materials provided with the distribution.
  12. //
  13. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  14. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  17. // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. //
  24. // The views and conclusions contained in the software and documentation are those
  25. // of the authors and should not be interpreted as representing official policies,
  26. // either expressed or implied, of the FreeBSD Project.
  27.  
  28. #ifndef LIST_H
  29. #define LIST_H
  30.  
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <sys/types.h>
  34. #include <sys/mman.h>
  35.  
  36.  
  37. #define list_qsort(listarg, comparator) qsort((listarg).list, (listarg).size, sizeof((listarg).list[0]), comparator)
  38.  
  39. #define list_t(type) struct { \
  40.     type *list; \
  41.     ssize_t size; \
  42.     size_t maxlength; \
  43. }
  44.  
  45. // initialises the list with 1024 elements base
  46. #define list_init(listarg) list_init_size( (listarg), 1024)
  47. // intialises the list with a different start size
  48. #define list_init_size( _list, initsize) list_init_size_alloc((_list), (initsize), sizeof((_list)->list[0]))
  49.  
  50. static inline void list_init_size_alloc( void * const _list, size_t initsize, size_t objectsize) {
  51.     list_t(void) * list = _list;
  52.     list->size = 0;
  53.     if(!(list->list = malloc(initsize * objectsize))) {
  54.         perror("malloc");
  55.     }
  56.     list->maxlength = initsize;
  57. }
  58.  
  59. // undefined behaviour if the list has not been initialised or freed before
  60. #define list_free(listarg) if ((listarg).list) free((listarg).list)
  61.  
  62. static inline int list_check_length_real(void * const _list, size_t objectsize) {
  63.     list_t(void) * list = _list;
  64.     if (list->size >= list->maxlength) { // enlarge the array
  65.        
  66.         void *tmp = realloc(list->list, list->maxlength * 2 * objectsize);
  67.         if (tmp) {
  68.             list->list = tmp;
  69.             list->maxlength *=2;
  70.         } else {
  71.             return -1;
  72.         }
  73.        
  74.     }
  75.     return 0;
  76. }
  77.  
  78. #define list_check_length(_list) list_check_length_real(_list, sizeof((_list)->list[0]))
  79.  
  80. // returns the index of a new zero-initialised entry
  81. #define list_new_empty( _list) (list_check_length(_list) ? -1 : (memset((_list)->list + (_list)->size, 0, sizeof((_list)->list[0])) , (_list)->size++))
  82.  
  83. // inserts an object into the list. List type and object type have to match
  84. #define list_insert( _list, object) (list_check_length(_list) ? -1 : (((_list)->list[(_list)->size] = (object)), (_list)->size++))
  85.  
  86. #define list_push list_insert
  87.  
  88. // undefined behaviour if the list is empty
  89. #define list_pop(_list) (_list)->list[--(_list)->size]
  90.  
  91. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement