Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

81 lines
2.6 KiB

  1. #ifdef __dietlibc__
  2. #include <sys/cdefs.h>
  3. #else
  4. #define __likely(x) x
  5. #define __unlikely(x) x
  6. #endif
  7. #include <sys/types.h>
  8. #include <stdlib.h>
  9. #include "safemult.h"
  10. #include "array.h"
  11. #include "byte.h"
  12. #if 0
  13. static array x;
  14. t *p;
  15. int64 pos;
  16. p = array_allocate(&x,sizeof(t),pos);
  17. array_allocate makes sure that enough bytes are allocated in x for at
  18. least pos+1 objects of type t. (The size of t must be positive;
  19. otherwise the effects are undefined.) If not enough bytes are
  20. allocated (or x is unallocated), array_allocate allocates more bytes,
  21. moving the dynamically allocated region if necessary. array_allocate
  22. often allocates somewhat more bytes than necessary, to save time
  23. later.
  24. array_allocate then makes sure that the number of bytes initialized
  25. covers at least those pos+1 objects. If not enough bytes are
  26. initialized, array_allocate initializes more bytes (setting them to
  27. 0), up to exactly the end of the pos+1st object.
  28. array_allocate then returns a pointer to the pos+1st object; i.e.,
  29. object number pos, with objects numbered starting at 0. This pointer
  30. can be used to change or inspect the object. The pointer can continue
  31. to be used through subsequent calls to array_get, array_start,
  32. array_length, and array_bytes, but it must not be used after any
  33. other operations on this array.
  34. If something goes wrong, array_allocate returns 0, setting errno
  35. appropriately, without touching x. In particular, array_allocate
  36. returns 0 if
  37. * x has failed, or
  38. * pos is negative, or
  39. * not enough memory is available.
  40. array_allocate does not change x to have failed; if you want to do
  41. that, use array_fail.
  42. #endif
  43. void* array_allocate(array* x,uint64 membersize,int64 pos) {
  44. uint64 wanted;
  45. if (__unlikely(x->allocated<0)) return 0; /* array is failed */
  46. if (__unlikely(pos+1<1)) return 0;
  47. /* second case of overflow: pos*membersize too large */
  48. if (__unlikely(umult64(membersize,pos+1,&wanted))) return 0;
  49. if (__unlikely(wanted > x->allocated)) {
  50. /* round up a little */
  51. if (membersize<8)
  52. wanted=(wanted+127)&(-128ll); /* round up to multiple of 128 */
  53. else
  54. wanted=(wanted+4095)&(-4096ll); /* round up to 4k pages */
  55. if (__unlikely(wanted<128)) return 0; /* overflow during rounding */
  56. if (sizeof(size_t) != sizeof(int64) && __unlikely((size_t)(wanted) != wanted))
  57. return 0;
  58. {
  59. char* tmp=realloc(x->p,wanted);
  60. if (__unlikely(!tmp)) return 0;
  61. x->p=tmp;
  62. }
  63. x->allocated=wanted;
  64. byte_zero(x->p+x->initialized,x->allocated-x->initialized);
  65. }
  66. x->initialized=pos*membersize;
  67. return x->p+pos*membersize;
  68. }