Guest User

Untitled

a guest
Jul 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. typedef struct {
  2. unsigned int count;
  3. unsigned int capacity;
  4. int *fds;
  5. } rb_fd_list;
  6.  
  7. int
  8. rb_fd_list_reset(rb_fd_list *list) {
  9. int *mem;
  10.  
  11. mem = realloc(list->fds, sizeof(int) * INITIAL_FD_LIST_CAPACITY);
  12. if (mem == NULL) {
  13. return 0;
  14. } else {
  15. list->fds = mem;
  16. list->capacity = INITIAL_FD_LIST_CAPACITY;
  17. list->count = 0;
  18. return 1;
  19. }
  20. }
  21.  
  22. int
  23. rb_fd_list_push(rb_fd_list *list, int fd) {
  24. size_t new_capacity;
  25. int *mem;
  26.  
  27. if (list->count >= list->capacity) {
  28. if (list->capacity == 0) {
  29. new_capacity = INITIAL_FD_LIST_CAPACITY;
  30. } else {
  31. new_capacity = list->capacity * 2;
  32. }
  33. mem = realloc(list->fds, sizeof(int) * new_capacity);
  34. if (mem == NULL) {
  35. return 0;
  36. } else {
  37. list->fds = mem;
  38. list->capacity = new_capacity;
  39. }
  40. }
  41. list->fds[list->count] = fd;
  42. list->count++;
  43. return 1;
  44. }
Add Comment
Please, Sign In to add comment