Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright (c) 2013, Sergey lamzin
- // All rights reserved.
- //
- // Redistribution and use in source and binary forms, with or without
- // modification, are permitted provided that the following conditions are met:
- //
- // 1. Redistributions of source code must retain the above copyright notice, this
- // list of conditions and the following disclaimer.
- // 2. Redistributions in binary form must reproduce the above copyright notice,
- // this list of conditions and the following disclaimer in the documentation
- // and/or other materials provided with the distribution.
- //
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
- // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- //
- // The views and conclusions contained in the software and documentation are those
- // of the authors and should not be interpreted as representing official policies,
- // either expressed or implied, of the FreeBSD Project.
- #ifndef LIST_H
- #define LIST_H
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/mman.h>
- #define list_qsort(listarg, comparator) qsort((listarg).list, (listarg).size, sizeof((listarg).list[0]), comparator)
- #define list_t(type) struct { \
- type *list; \
- ssize_t size; \
- size_t maxlength; \
- }
- // initialises the list with 1024 elements base
- #define list_init(listarg) list_init_size( (listarg), 1024)
- // intialises the list with a different start size
- #define list_init_size( _list, initsize) list_init_size_alloc((_list), (initsize), sizeof((_list)->list[0]))
- static inline void list_init_size_alloc( void * const _list, size_t initsize, size_t objectsize) {
- list_t(void) * list = _list;
- list->size = 0;
- if(!(list->list = malloc(initsize * objectsize))) {
- perror("malloc");
- }
- list->maxlength = initsize;
- }
- // undefined behaviour if the list has not been initialised or freed before
- #define list_free(listarg) if ((listarg).list) free((listarg).list)
- static inline int list_check_length_real(void * const _list, size_t objectsize) {
- list_t(void) * list = _list;
- if (list->size >= list->maxlength) { // enlarge the array
- void *tmp = realloc(list->list, list->maxlength * 2 * objectsize);
- if (tmp) {
- list->list = tmp;
- list->maxlength *=2;
- } else {
- return -1;
- }
- }
- return 0;
- }
- #define list_check_length(_list) list_check_length_real(_list, sizeof((_list)->list[0]))
- // returns the index of a new zero-initialised entry
- #define list_new_empty( _list) (list_check_length(_list) ? -1 : (memset((_list)->list + (_list)->size, 0, sizeof((_list)->list[0])) , (_list)->size++))
- // inserts an object into the list. List type and object type have to match
- #define list_insert( _list, object) (list_check_length(_list) ? -1 : (((_list)->list[(_list)->size] = (object)), (_list)->size++))
- #define list_push list_insert
- // undefined behaviour if the list is empty
- #define list_pop(_list) (_list)->list[--(_list)->size]
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement