]> mj.ucw.cz Git - libucw.git/commitdiff
Merge branch 'master' into dev-sizet
authorPavel Charvat <pchar@ucw.cz>
Wed, 4 Jun 2014 00:08:15 +0000 (02:08 +0200)
committerPavel Charvat <pchar@ucw.cz>
Wed, 4 Jun 2014 00:08:15 +0000 (02:08 +0200)
Conflicts:
ucw/bbuf.c
ucw/bbuf.h
ucw/doc/mempool.txt
ucw/fastbuf.h
ucw/fb-pool.c
ucw/gbuf.h
ucw/io-mmap.c
ucw/io.h
ucw/mempool-fmt.c
ucw/mempool-str.c
ucw/mempool.c
ucw/mempool.h
ucw/str-hex.c
ucw/string.c
ucw/string.h
ucw/unicode.c
ucw/unicode.h

18 files changed:
ucw/bbuf.c
ucw/bbuf.h
ucw/doc/mempool.txt
ucw/fastbuf.h
ucw/fb-pool.c
ucw/gbuf.h
ucw/io-careful.c
ucw/io-mmap.c
ucw/io.h
ucw/mempool-fmt.c
ucw/mempool-str.c
ucw/mempool.c
ucw/mempool.h
ucw/str-hex.c
ucw/string.c
ucw/string.h
ucw/unicode.c
ucw/unicode.h

index e055afd8c3fc7629768c00923fde6e8022ff9505..36ece2a066794629be17c20a87b50730fa8108ee 100644 (file)
@@ -13,7 +13,7 @@
 #include <stdio.h>
 
 char *
-bb_vprintf_at(bb_t *bb, uint ofs, const char *fmt, va_list args)
+bb_vprintf_at(bb_t *bb, size_t ofs, const char *fmt, va_list args)
 {
   bb_grow(bb, ofs + 1);
   va_list args2;
@@ -44,7 +44,7 @@ bb_vprintf_at(bb_t *bb, uint ofs, const char *fmt, va_list args)
 }
 
 char *
-bb_printf_at(bb_t *bb, uint ofs, const char *fmt, ...)
+bb_printf_at(bb_t *bb, size_t ofs, const char *fmt, ...)
 {
   va_list args;
   va_start(args, fmt);
index c267d42ee4883bf7688d774bdfa00e43ea7751ef..0cbccbc2874a26527e9c35006d2278bea7b1a517 100644 (file)
@@ -29,6 +29,7 @@
  * See @bb_printf().
  **/
 char *bb_vprintf(bb_t *bb, const char *fmt, va_list args);
+
 /**
  * printf() into a growing buffer.
  * Generates a `'\0'`-terminated string at the beginning of the buffer
@@ -37,6 +38,7 @@ char *bb_vprintf(bb_t *bb, const char *fmt, va_list args);
  * See @bb_vprintf().
  **/
 char *bb_printf(bb_t *bb, const char *fmt, ...);
+
 /**
  * Like @bb_vprintf(), but it does not start at the beginning of the
  * buffer, but @ofs bytes further.
@@ -44,10 +46,11 @@ char *bb_printf(bb_t *bb, const char *fmt, ...);
  * Returns pointer to the new string (eg. @ofs bytes after the
  * beginning of buffer).
  **/
-char *bb_vprintf_at(bb_t *bb, uint ofs, const char *fmt, va_list args);
+char *bb_vprintf_at(bb_t *bb, size_t ofs, const char *fmt, va_list args);
+
 /**
  * Like @bb_vprintf_at(), but it takes individual arguments.
  **/
-char *bb_printf_at(bb_t *bb, uint ofs, const char *fmt, ...);
+char *bb_printf_at(bb_t *bb, size_t ofs, const char *fmt, ...);
 
 #endif
index 62e06d6701e9441097a1f1e2d49d345146200928..5aa33ddd2e981f4c8d219ceb1841ecfe828c2639 100644 (file)
@@ -112,7 +112,7 @@ This example uses libucw's own IO system, <<fastbuf:,fastbufs>>.
 
   void *stdin_data(struct mempool *pool) {
     struct fastbuf *fb = bopen_fd(0, NULL);     // Read from stdin
-    uint amount;
+    size_t amount;
     char *ptr = mp_start(pool, 1024);
     while(amount = bread(fb, ptr, 1024)) {      // Read a block
       ptr += amount;                            // Move after it
index 8db17b62ecece53ecb20e2ce04eca76b88519503..91c42b85fc4f2524f89fa537457c2b3aeecd771b 100644 (file)
@@ -481,7 +481,7 @@ void fbpool_init(struct fbpool *fb);        /** Initialize a new mempool fastbuf. **/
  * Start a new continuous block and prepare for writing (see <<mempool:mp_start()>>).
  * Provide the memory pool you want to use for this block as @mp.
  **/
-void fbpool_start(struct fbpool *fb, struct mempool *mp, uint init_size);
+void fbpool_start(struct fbpool *fb, struct mempool *mp, size_t init_size);
 /**
  * Close the block and return the address of its start (see <<mempool:mp_end()>>).
  * The length can be determined by calling <<mempool:mp_size(mp, ptr)>>.
index d95e5c04b43fce4603249f45de6d4150b2cbcea2..01ce3443dcbd6b69dd97aadb93a7d00655176ed7 100644 (file)
@@ -29,7 +29,7 @@ fbpool_spout(struct fastbuf *b)
 }
 
 void
-fbpool_start(struct fbpool *b, struct mempool *mp, uint init_size)
+fbpool_start(struct fbpool *b, struct mempool *mp, size_t init_size)
 {
   b->mp = mp;
   b->fb.buffer = b->fb.bstop = b->fb.bptr = mp_start(mp, init_size);
index 4e01556805ee08f28217da453b988d2c7163b0f6..1f5e68fa1f83abed3a4ea4c0776a2c9d6e60f650 100644 (file)
@@ -27,7 +27,7 @@
  * length of available memory.
  **/
 typedef struct BUF_T {
-  uint len;
+  size_t len;
   GBUF_TYPE *ptr;
 } BUF_T;
 
@@ -60,16 +60,16 @@ static void UNUSED GBUF_PREFIX(done)(BUF_T *b)
  * Use <<fun__GENERIC_LINK_|GBUF_PREFIX|grow|,`GBUF_PREFIX(grow)()`>>
  * for growing.
  **/
-static void UNUSED GBUF_PREFIX(set_size)(BUF_T *b, uint len)
+static void UNUSED GBUF_PREFIX(set_size)(BUF_T *b, size_t len)
 {
   b->len = len;
   b->ptr = xrealloc(b->ptr, len * sizeof(GBUF_TYPE));
 #ifdef GBUF_TRACE
-  GBUF_TRACE(STRINGIFY_EXPANDED(BUF_T) " growing to %u items", len);
+  GBUF_TRACE(STRINGIFY_EXPANDED(BUF_T) " growing to %zu items", len);
 #endif
 }
 
-static void UNUSED GBUF_PREFIX(do_grow)(BUF_T *b, uint len)
+static void UNUSED GBUF_PREFIX(do_grow)(BUF_T *b, size_t len)
 {
   if (len < 2*b->len)                  // to ensure logarithmic cost
     len = 2*b->len;
@@ -85,7 +85,7 @@ static void UNUSED GBUF_PREFIX(do_grow)(BUF_T *b, uint len)
  * any more) by
  * <<fun__GENERIC_LINK_|GBUF_PREFIX|set_size|,`GBUF_PREFIX(set_size)()`>>.
  **/
-static inline GBUF_TYPE *GBUF_PREFIX(grow)(BUF_T *b, uint len)
+static inline GBUF_TYPE *GBUF_PREFIX(grow)(BUF_T *b, size_t len)
 {
   if (unlikely(len > b->len))
     GBUF_PREFIX(do_grow)(b, len);
index b12b71141182b993f8d77d7695983ffefd996eed..a3a8a8064e7ab5cada60fabd988ebad212d51261 100644 (file)
  */
 
 int
-careful_read(int fd, void *buf, int len)
+careful_read(int fd, void *buf, size_t len)
 {
   byte *pos = buf;
   while (len)
     {
-      int l = read(fd, pos, len);
+      ssize_t l = read(fd, pos, len);
       if (l < 0)
        return -1;
       if (!l)
@@ -35,12 +35,12 @@ careful_read(int fd, void *buf, int len)
 }
 
 int
-careful_write(int fd, const void *buf, int len)
+careful_write(int fd, const void *buf, size_t len)
 {
   const byte *pos = buf;
   while (len)
     {
-      int l = write(fd, pos, len);
+      ssize_t l = write(fd, pos, len);
       if (l < 0)
        return -1;
       if (!l)
index 4e3a8ca17cd656a8bb306eb1a006ee7c85e32df5..1a3f1438102f2371da3ec2ecfde5ef9626159c78 100644 (file)
 #include <sys/mman.h>
 
 void *
-mmap_file(const char *name, uint *len, int writeable)
+mmap_file(const char *name, size_t *len, int writeable)
 {
-  int fd = open(name, writeable ? O_RDWR : O_RDONLY);
-  struct stat st;
+  int fd = ucw_open(name, writeable ? O_RDWR : O_RDONLY);
+  ucw_stat_t st;
   void *x;
 
   if (fd < 0)
     die("open(%s): %m", name);
-  if (fstat(fd, &st) < 0)
+  if (ucw_fstat(fd, &st) < 0)
     die("fstat(%s): %m", name);
+  if ((uintmax_t)st.st_size > SIZE_MAX)
+    die("mmap(%s): File too large", name);
   if (len)
     *len = st.st_size;
   if (st.st_size)
@@ -42,7 +44,7 @@ mmap_file(const char *name, uint *len, int writeable)
 }
 
 void
-munmap_file(void *start, uint len)
+munmap_file(void *start, size_t len)
 {
   munmap(start, len);
 }
index 710274d756c0086930e2944ae605cb4c53b4a216..ac8c7ec67df834670e0f32293d564b3cbe5a3b26 100644 (file)
--- a/ucw/io.h
+++ b/ucw/io.h
@@ -65,13 +65,13 @@ ucw_off_t ucw_file_size(const char *name);
 
 /* io-mmap.c */
 
-void *mmap_file(const char *name, uint *len, int writeable);
-void munmap_file(void *start, uint len);
+void *mmap_file(const char *name, size_t *len, int writeable);
+void munmap_file(void *start, size_t len);
 
 /* io-careful.c */
 
-int careful_read(int fd, void *buf, int len);
-int careful_write(int fd, const void *buf, int len);
+int careful_read(int fd, void *buf, size_t len);
+int careful_write(int fd, const void *buf, size_t len);
 
 /* io-sync.c */
 
index cba7f8260eb9a097c7e68c47c427c0d977866def..1b98a2d39df9d5ffabac8f6b17b983fc9f585271 100644 (file)
@@ -16,7 +16,7 @@
 #include <string.h>
 
 static char *
-mp_vprintf_at(struct mempool *mp, uint ofs, const char *fmt, va_list args)
+mp_vprintf_at(struct mempool *mp, size_t ofs, const char *fmt, va_list args)
 {
   char *ret = mp_grow(mp, ofs + 1) + ofs;
   va_list args2;
@@ -67,7 +67,7 @@ mp_printf(struct mempool *p, const char *fmt, ...)
 char *
 mp_append_vprintf(struct mempool *mp, char *ptr, const char *fmt, va_list args)
 {
-  uint ofs = mp_open(mp, ptr);
+  size_t ofs = mp_open(mp, ptr);
   ASSERT(ofs && !ptr[ofs - 1]);
   return mp_vprintf_at(mp, ofs - 1, fmt, args);
 }
index 26a8b3bc7416e28f4f01f122e101a870a2cd7b81..1072e3dacb7fde29a0deceef692d77732478b6fa 100644 (file)
@@ -18,14 +18,14 @@ mp_strdup(struct mempool *p, const char *s)
 {
   if (!s)
     return NULL;
-  uint l = strlen(s) + 1;
+  size_t l = strlen(s) + 1;
   char *t = mp_alloc_fast_noalign(p, l);
   memcpy(t, s, l);
   return t;
 }
 
 void *
-mp_memdup(struct mempool *p, const void *s, uint len)
+mp_memdup(struct mempool *p, const void *s, size_t len)
 {
   void *t = mp_alloc_fast(p, len);
   memcpy(t, s, len);
@@ -42,8 +42,8 @@ mp_multicat(struct mempool *p, ...)
   va_copy(a, args);
   while (x = va_arg(a, char *))
     cnt++;
-  uint *sizes = alloca(cnt * sizeof(uint));
-  uint len = 1;
+  size_t *sizes = alloca(cnt * sizeof(*sizes));
+  size_t len = 1;
   cnt = 0;
   va_end(a);
   va_copy(a, args);
@@ -66,8 +66,8 @@ mp_multicat(struct mempool *p, ...)
 char *
 mp_strjoin(struct mempool *p, char **a, uint n, uint sep)
 {
-  uint sizes[n];
-  uint len = 1;
+  size_t sizes[n];
+  size_t len = 1;
   for (uint i=0; i<n; i++)
     len += sizes[i] = strlen(a[i]);
   if (sep && n)
@@ -86,7 +86,7 @@ mp_strjoin(struct mempool *p, char **a, uint n, uint sep)
 }
 
 char *
-mp_str_from_mem(struct mempool *a, const void *mem, uint len)
+mp_str_from_mem(struct mempool *a, const void *mem, size_t len)
 {
   char *str = mp_alloc_noalign(a, len+1);
   memcpy(str, mem, len);
index 086e6598c31a1ba28b64a8cde19489aa8768df54..f9e268dab5c9144745eb80b6bc735abcb9a30933 100644 (file)
 #include <string.h>
 
 #define MP_CHUNK_TAIL ALIGN_TO(sizeof(struct mempool_chunk), CPU_STRUCT_ALIGN)
-#define MP_SIZE_MAX (~0U - MP_CHUNK_TAIL - CPU_PAGE_SIZE)
+#define MP_SIZE_MAX (SIZE_MAX - MP_CHUNK_TAIL - CPU_PAGE_SIZE)
 
 struct mempool_chunk {
 #ifdef CONFIG_DEBUG
   struct mempool *pool;                // Can be useful when analysing coredump for memory leaks
 #endif
   struct mempool_chunk *next;
-  uint size;
+  size_t size;
 };
 
-static uint
-mp_align_size(uint size)
+static size_t
+mp_align_size(size_t size)
 {
 #ifdef CONFIG_UCW_POOL_IS_MMAP
   return ALIGN_TO(size + MP_CHUNK_TAIL, CPU_PAGE_SIZE) - MP_CHUNK_TAIL;
@@ -64,7 +64,7 @@ static void mp_allocator_free(struct ucw_allocator *a UNUSED, void *ptr UNUSED)
 }
 
 void
-mp_init(struct mempool *pool, uint chunk_size)
+mp_init(struct mempool *pool, size_t chunk_size)
 {
   chunk_size = mp_align_size(MAX(sizeof(struct mempool), chunk_size));
   *pool = (struct mempool) {
@@ -80,7 +80,7 @@ mp_init(struct mempool *pool, uint chunk_size)
 }
 
 static void *
-mp_new_big_chunk(struct mempool *pool, uint size)
+mp_new_big_chunk(struct mempool *pool, size_t size)
 {
   struct mempool_chunk *chunk;
   chunk = xmalloc(size + MP_CHUNK_TAIL) + size;
@@ -98,7 +98,7 @@ mp_free_big_chunk(struct mempool *pool, struct mempool_chunk *chunk)
 }
 
 static void *
-mp_new_chunk(struct mempool *pool, uint size)
+mp_new_chunk(struct mempool *pool, size_t size)
 {
 #ifdef CONFIG_UCW_POOL_IS_MMAP
   struct mempool_chunk *chunk;
@@ -124,7 +124,7 @@ mp_free_chunk(struct mempool *pool, struct mempool_chunk *chunk)
 }
 
 struct mempool *
-mp_new(uint chunk_size)
+mp_new(size_t chunk_size)
 {
   chunk_size = mp_align_size(MAX(sizeof(struct mempool), chunk_size));
   struct mempool_chunk *chunk = mp_new_chunk(NULL, chunk_size);
@@ -249,7 +249,7 @@ mp_shrink(struct mempool *pool, u64 min_total_size)
 }
 
 void *
-mp_alloc_internal(struct mempool *pool, uint size)
+mp_alloc_internal(struct mempool *pool, size_t size)
 {
   struct mempool_chunk *chunk;
   if (size <= pool->threshold)
@@ -275,7 +275,7 @@ mp_alloc_internal(struct mempool *pool, uint size)
   else if (likely(size <= MP_SIZE_MAX))
     {
       pool->idx = 1;
-      uint aligned = ALIGN_TO(size, CPU_STRUCT_ALIGN);
+      size_t aligned = ALIGN_TO(size, CPU_STRUCT_ALIGN);
       chunk = mp_new_big_chunk(pool, aligned);
       chunk->next = pool->state.last[1];
 #ifdef CONFIG_DEBUG
@@ -286,23 +286,23 @@ mp_alloc_internal(struct mempool *pool, uint size)
       return pool->last_big = (void *)chunk - aligned;
     }
   else
-    die("Cannot allocate %u bytes from a mempool", size);
+    die("Cannot allocate %zu bytes from a mempool", size);
 }
 
 void *
-mp_alloc(struct mempool *pool, uint size)
+mp_alloc(struct mempool *pool, size_t size)
 {
   return mp_alloc_fast(pool, size);
 }
 
 void *
-mp_alloc_noalign(struct mempool *pool, uint size)
+mp_alloc_noalign(struct mempool *pool, size_t size)
 {
   return mp_alloc_fast_noalign(pool, size);
 }
 
 void *
-mp_alloc_zero(struct mempool *pool, uint size)
+mp_alloc_zero(struct mempool *pool, size_t size)
 {
   void *ptr = mp_alloc_fast(pool, size);
   bzero(ptr, size);
@@ -310,7 +310,7 @@ mp_alloc_zero(struct mempool *pool, uint size)
 }
 
 void *
-mp_start_internal(struct mempool *pool, uint size)
+mp_start_internal(struct mempool *pool, size_t size)
 {
   void *ptr = mp_alloc_internal(pool, size);
   pool->state.free[pool->idx] += size;
@@ -318,27 +318,27 @@ mp_start_internal(struct mempool *pool, uint size)
 }
 
 void *
-mp_start(struct mempool *pool, uint size)
+mp_start(struct mempool *pool, size_t size)
 {
   return mp_start_fast(pool, size);
 }
 
 void *
-mp_start_noalign(struct mempool *pool, uint size)
+mp_start_noalign(struct mempool *pool, size_t size)
 {
   return mp_start_fast_noalign(pool, size);
 }
 
 void *
-mp_grow_internal(struct mempool *pool, uint size)
+mp_grow_internal(struct mempool *pool, size_t size)
 {
   if (unlikely(size > MP_SIZE_MAX))
-    die("Cannot allocate %u bytes of memory", size);
-  uint avail = mp_avail(pool);
+    die("Cannot allocate %zu bytes of memory", size);
+  size_t avail = mp_avail(pool);
   void *ptr = mp_ptr(pool);
   if (pool->idx)
     {
-      uint amortized = likely(avail <= MP_SIZE_MAX / 2) ? avail * 2 : MP_SIZE_MAX;
+      size_t amortized = likely(avail <= MP_SIZE_MAX / 2) ? avail * 2 : MP_SIZE_MAX;
       amortized = MAX(amortized, size);
       amortized = ALIGN_TO(amortized, CPU_STRUCT_ALIGN);
       struct mempool_chunk *chunk = pool->state.last[1], *next = chunk->next;
@@ -360,22 +360,22 @@ mp_grow_internal(struct mempool *pool, uint size)
     }
 }
 
-uint
+size_t
 mp_open(struct mempool *pool, void *ptr)
 {
   return mp_open_fast(pool, ptr);
 }
 
 void *
-mp_realloc(struct mempool *pool, void *ptr, uint size)
+mp_realloc(struct mempool *pool, void *ptr, size_t size)
 {
   return mp_realloc_fast(pool, ptr, size);
 }
 
 void *
-mp_realloc_zero(struct mempool *pool, void *ptr, uint size)
+mp_realloc_zero(struct mempool *pool, void *ptr, size_t size)
 {
-  uint old_size = mp_open_fast(pool, ptr);
+  size_t old_size = mp_open_fast(pool, ptr);
   ptr = mp_grow(pool, size);
   if (size > old_size)
     bzero(ptr + old_size, size - old_size);
@@ -384,7 +384,7 @@ mp_realloc_zero(struct mempool *pool, void *ptr, uint size)
 }
 
 void *
-mp_spread_internal(struct mempool *pool, void *p, uint size)
+mp_spread_internal(struct mempool *pool, void *p, size_t size)
 {
   void *old = mp_ptr(pool);
   void *new = mp_grow_internal(pool, p-old+size);
index 28ce680af772e9075d8537ec1dc27c8cf9e25069..59ef22960e0df875f8beaec5f99b69dcaea406c7 100644 (file)
@@ -58,7 +58,7 @@
  * You should use this one as an opaque handle only, the insides are internal.
  **/
 struct mempool_state {
-  uint free[2];
+  size_t free[2];
   void *last[2];
   struct mempool_state *next;
 };
@@ -71,7 +71,8 @@ struct mempool {
   struct ucw_allocator allocator;
   struct mempool_state state;
   void *unused, *last_big;
-  uint chunk_size, threshold, idx;
+  size_t chunk_size, threshold;
+  uns idx;
   u64 total_size;
 };
 
@@ -90,13 +91,13 @@ struct mempool_stats {                      /** Mempool statistics. See @mp_stats(). **/
 
 /**
  * Initialize a given mempool structure.
- * @chunk_size must be in the interval `[1, UINT_MAX / 2]`.
+ * @chunk_size must be in the interval `[1, SIZE_MAX / 2]`.
  * It will allocate memory by this large chunks and take
  * memory to satisfy requests from them.
  *
  * Memory pools can be treated as <<trans:respools,resources>>, see <<trans:res_mempool()>>.
  **/
-void mp_init(struct mempool *pool, uint chunk_size);
+void mp_init(struct mempool *pool, size_t chunk_size);
 
 /**
  * Allocate and initialize a new memory pool.
@@ -106,7 +107,7 @@ void mp_init(struct mempool *pool, uint chunk_size);
  *
  * Memory pools can be treated as <<trans:respools,resources>>, see <<trans:res_mempool()>>.
  **/
-struct mempool *mp_new(uint chunk_size);
+struct mempool *mp_new(size_t chunk_size);
 
 /**
  * Cleanup mempool initialized by mp_init or mp_new.
@@ -150,7 +151,7 @@ void mp_shrink(struct mempool *pool, u64 min_total_size);
  ***/
 
 /* For internal use only, do not call directly */
-void *mp_alloc_internal(struct mempool *pool, uint size) LIKE_MALLOC;
+void *mp_alloc_internal(struct mempool *pool, size_t size) LIKE_MALLOC;
 
 /**
  * The function allocates new @size bytes on a given memory pool.
@@ -162,24 +163,24 @@ void *mp_alloc_internal(struct mempool *pool, uint size) LIKE_MALLOC;
  * `CPU_STRUCT_ALIGN` bytes and this condition remains true also
  * after future reallocations.
  **/
-void *mp_alloc(struct mempool *pool, uint size);
+void *mp_alloc(struct mempool *pool, size_t size);
 
 /**
  * The same as @mp_alloc(), but the result may be unaligned.
  **/
-void *mp_alloc_noalign(struct mempool *pool, uint size);
+void *mp_alloc_noalign(struct mempool *pool, size_t size);
 
 /**
  * The same as @mp_alloc(), but fills the newly allocated memory with zeroes.
  **/
-void *mp_alloc_zero(struct mempool *pool, uint size);
+void *mp_alloc_zero(struct mempool *pool, size_t size);
 
 /**
  * Inlined version of @mp_alloc().
  **/
-static inline void *mp_alloc_fast(struct mempool *pool, uint size)
+static inline void *mp_alloc_fast(struct mempool *pool, size_t size)
 {
-  uint avail = pool->state.free[0] & ~(CPU_STRUCT_ALIGN - 1);
+  size_t avail = pool->state.free[0] & ~(size_t)(CPU_STRUCT_ALIGN - 1);
   if (size <= avail)
     {
       pool->state.free[0] = avail - size;
@@ -192,7 +193,7 @@ static inline void *mp_alloc_fast(struct mempool *pool, uint size)
 /**
  * Inlined version of @mp_alloc_noalign().
  **/
-static inline void *mp_alloc_fast_noalign(struct mempool *pool, uint size)
+static inline void *mp_alloc_fast_noalign(struct mempool *pool, size_t size)
 {
   if (size <= pool->state.free[0])
     {
@@ -225,9 +226,9 @@ static inline struct ucw_allocator *mp_get_allocator(struct mempool *mp)
  ***/
 
 /* For internal use only, do not call directly */
-void *mp_start_internal(struct mempool *pool, uint size) LIKE_MALLOC;
-void *mp_grow_internal(struct mempool *pool, uint size);
-void *mp_spread_internal(struct mempool *pool, void *p, uint size);
+void *mp_start_internal(struct mempool *pool, size_t size) LIKE_MALLOC;
+void *mp_grow_internal(struct mempool *pool, size_t size);
+void *mp_spread_internal(struct mempool *pool, void *p, size_t size);
 
 static inline uint mp_idx(struct mempool *pool, void *ptr)
 {
@@ -247,15 +248,15 @@ static inline uint mp_idx(struct mempool *pool, void *ptr)
  * Keep in mind that you can't make any other pool allocations
  * before you "close" the growing buffer with @mp_end().
  */
-void *mp_start(struct mempool *pool, uint size);
-void *mp_start_noalign(struct mempool *pool, uint size);
+void *mp_start(struct mempool *pool, size_t size);
+void *mp_start_noalign(struct mempool *pool, size_t size);
 
 /**
  * Inlined version of @mp_start().
  **/
-static inline void *mp_start_fast(struct mempool *pool, uint size)
+static inline void *mp_start_fast(struct mempool *pool, size_t size)
 {
-  uint avail = pool->state.free[0] & ~(CPU_STRUCT_ALIGN - 1);
+  size_t avail = pool->state.free[0] & ~(size_t)(CPU_STRUCT_ALIGN - 1);
   if (size <= avail)
     {
       pool->idx = 0;
@@ -269,7 +270,7 @@ static inline void *mp_start_fast(struct mempool *pool, uint size)
 /**
  * Inlined version of @mp_start_noalign().
  **/
-static inline void *mp_start_fast_noalign(struct mempool *pool, uint size)
+static inline void *mp_start_fast_noalign(struct mempool *pool, size_t size)
 {
   if (size <= pool->state.free[0])
     {
@@ -292,7 +293,7 @@ static inline void *mp_ptr(struct mempool *pool)
  * Return the number of bytes available for extending the growing buffer.
  * (Before a reallocation will be needed).
  **/
-static inline uint mp_avail(struct mempool *pool)
+static inline size_t mp_avail(struct mempool *pool)
 {
   return pool->state.free[pool->idx];
 }
@@ -303,7 +304,7 @@ static inline uint mp_avail(struct mempool *pool)
  * change its starting position. The content will be unchanged to the minimum
  * of the old and new sizes; newly allocated memory will be uninitialized.
  * Multiple calls to mp_grow() have amortized linear cost wrt. the maximum value of @size. */
-static inline void *mp_grow(struct mempool *pool, uint size)
+static inline void *mp_grow(struct mempool *pool, size_t size)
 {
   return (size <= mp_avail(pool)) ? mp_ptr(pool) : mp_grow_internal(pool, size);
 }
@@ -320,9 +321,9 @@ static inline void *mp_expand(struct mempool *pool)
  * Ensure that there is at least @size bytes free after @p,
  * if not, reallocate and adjust @p.
  **/
-static inline void *mp_spread(struct mempool *pool, void *p, uint size)
+static inline void *mp_spread(struct mempool *pool, void *p, size_t size)
 {
-  return (((uint)((byte *)pool->state.last[pool->idx] - (byte *)p) >= size) ? p : mp_spread_internal(pool, p, size));
+  return (((size_t)((byte *)pool->state.last[pool->idx] - (byte *)p) >= size) ? p : mp_spread_internal(pool, p, size));
 }
 
 /**
@@ -342,7 +343,7 @@ static inline char *mp_append_char(struct mempool *pool, char *p, uint c)
  * the last byte in the buffer, returns a pointer after the last byte
  * of the new (possibly reallocated) buffer.
  **/
-static inline void *mp_append_block(struct mempool *pool, void *p, const void *block, uint size)
+static inline void *mp_append_block(struct mempool *pool, void *p, const void *block, size_t size)
 {
   char *q = mp_spread(pool, p, size);
   memcpy(q, block, size);
@@ -383,7 +384,7 @@ static inline char *mp_end_string(struct mempool *pool, void *end)
 /**
  * Return size in bytes of the last allocated memory block (with @mp_alloc() or @mp_end()).
  **/
-static inline uint mp_size(struct mempool *pool, void *ptr)
+static inline size_t mp_size(struct mempool *pool, void *ptr)
 {
   uint idx = mp_idx(pool, ptr);
   return ((byte *)pool->state.last[idx] - (byte *)ptr) - pool->state.free[idx];
@@ -394,15 +395,15 @@ static inline uint mp_size(struct mempool *pool, void *ptr)
  * for growing and return its size in bytes. The contents and the start pointer
  * remain unchanged. Do not forget to call @mp_end() to close it.
  **/
-uint mp_open(struct mempool *pool, void *ptr);
+size_t mp_open(struct mempool *pool, void *ptr);
 
 /**
  * Inlined version of @mp_open().
  **/
-static inline uint mp_open_fast(struct mempool *pool, void *ptr)
+static inline size_t mp_open_fast(struct mempool *pool, void *ptr)
 {
   pool->idx = mp_idx(pool, ptr);
-  uint size = ((byte *)pool->state.last[pool->idx] - (byte *)ptr) - pool->state.free[pool->idx];
+  size_t size = ((byte *)pool->state.last[pool->idx] - (byte *)ptr) - pool->state.free[pool->idx];
   pool->state.free[pool->idx] += size;
   return size;
 }
@@ -412,17 +413,17 @@ static inline uint mp_open_fast(struct mempool *pool, void *ptr)
  * to the new @size. Behavior is similar to @mp_grow(), but the resulting
  * block is closed.
  **/
-void *mp_realloc(struct mempool *pool, void *ptr, uint size);
+void *mp_realloc(struct mempool *pool, void *ptr, size_t size);
 
 /**
  * The same as @mp_realloc(), but fills the additional bytes (if any) with zeroes.
  **/
-void *mp_realloc_zero(struct mempool *pool, void *ptr, uint size);
+void *mp_realloc_zero(struct mempool *pool, void *ptr, size_t size);
 
 /**
  * Inlined version of @mp_realloc().
  **/
-static inline void *mp_realloc_fast(struct mempool *pool, void *ptr, uint size)
+static inline void *mp_realloc_fast(struct mempool *pool, void *ptr, size_t size)
 {
   mp_open_fast(pool, ptr);
   ptr = mp_grow(pool, size);
@@ -490,7 +491,7 @@ void mp_pop(struct mempool *pool);
  ***/
 
 char *mp_strdup(struct mempool *, const char *) LIKE_MALLOC;           /** Makes a copy of a string on a mempool. Returns NULL for NULL string. **/
-void *mp_memdup(struct mempool *, const void *, uint) LIKE_MALLOC;     /** Makes a copy of a memory block on a mempool. **/
+void *mp_memdup(struct mempool *, const void *, size_t) LIKE_MALLOC;   /** Makes a copy of a memory block on a mempool. **/
 /**
  * Concatenates all passed strings. The last parameter must be NULL.
  * This will concatenate two strings:
@@ -515,7 +516,7 @@ char *mp_strjoin(struct mempool *p, char **a, uint n, uint sep) LIKE_MALLOC;
  * Convert memory block to a string. Makes a copy of the given memory block
  * in the mempool @p, adding an extra terminating zero byte at the end.
  **/
-char *mp_str_from_mem(struct mempool *p, const void *mem, uint len) LIKE_MALLOC;
+char *mp_str_from_mem(struct mempool *p, const void *mem, size_t len) LIKE_MALLOC;
 
 
 /***
index 8cf27cb2d34c76e70e9ebe3ccb5301fb17652545..f8595edfb8a233213fd3479afa9d9d11ac182cf0 100644 (file)
@@ -18,7 +18,7 @@ hex_make(uint x)
 }
 
 void
-mem_to_hex(char *dest, const byte *src, uint bytes, uint flags)
+mem_to_hex(char *dest, const byte *src, size_t bytes, uns flags)
 {
   uint sep = flags & 0xff;
 
@@ -48,7 +48,7 @@ hex_parse(uint c)
 }
 
 const char *
-hex_to_mem(byte *dest, const char *src, uint max_bytes, uint flags)
+hex_to_mem(byte *dest, const char *src, size_t max_bytes, uns flags)
 {
   uint sep = flags & 0xff;
   while (max_bytes-- && Cxdigit(src[0]) && Cxdigit(src[1]))
index f9877acacdb3bda77eb8b7f8adaae6dd6a9b4862..21556ec3ba423a5ed878d2b1ed4207d7718b0b2f 100644 (file)
@@ -14,8 +14,8 @@
 #include <ucw/string.h>
 
 #ifdef CONFIG_DARWIN
-uint
-strnlen(const char *str, uint n)
+size_t
+strnlen(const char *str, size_t n)
 {
   const char *end = str + n;
   const char *c;
@@ -39,11 +39,11 @@ str_format_flags(char *dest, const char *fmt, uint flags)
   return start;
 }
 
-uint
-str_count_char(const char *str, uint chr)
+size_t
+str_count_char(const char *str, uns chr)
 {
   const byte *s = str;
-  uint i = 0;
+  size_t i = 0;
   while (*s)
     if (*s++ == chr)
       i++;
index a38ab938f4f6b32331f3197952690ac488ac326f..b49370edb056b33c1bfc2cac8d2c065b66041467 100644 (file)
@@ -30,7 +30,7 @@
 /* string.c */
 
 #ifdef CONFIG_DARWIN
-uint strnlen(const char *str, uint n); // NOAPI
+size_t strnlen(const char *str, size_t n); // NOAPI
 #endif
 
 /**
@@ -40,7 +40,7 @@ uint strnlen(const char *str, uint n);        // NOAPI
 char *str_format_flags(char *dest, const char *fmt, uint flags);
 
 /** Counts occurrences of @chr in @str. **/
-uint str_count_char(const char *str, uint chr);
+size_t str_count_char(const char *str, uns chr);
 
 /* str-esc.c */
 
@@ -94,14 +94,14 @@ int str_match_pattern_nocase(const char *patt, const char *str);
  * not be separated), possibly OR-ed with `MEM_TO_HEX_UPCASE` when upper-case
  * characters should be used.
  **/
-void mem_to_hex(char *dest, const byte *src, uint bytes, uint flags);
+void mem_to_hex(char *dest, const byte *src, size_t bytes, uns flags);
 
 /**
  * An inverse function to @mem_to_hex(). Takes a hexdump of at most @max_bytes
  * bytes and stores the bytes to a buffer starting at @dest. Returns a pointer
  * at the first character after the dump.
  **/
-const char *hex_to_mem(byte *dest, const char *src, uint max_bytes, uint flags);
+const char *hex_to_mem(byte *dest, const char *src, size_t max_bytes, uns flags);
 
 // Bottom 8 bits of flags are an optional separator of bytes, the rest is:
 #define MEM_TO_HEX_UPCASE 0x100
index fcff81cfc2ea41155d6afb2d08d18132f125e0a1..8615f122b22fb65ba9aa30ca9bf3592791a3155a 100644 (file)
 #include <ucw/lib.h>
 #include <ucw/unicode.h>
 
-uint
+size_t
 utf8_strlen(const byte *str)
 {
-  uint len = 0;
+  size_t len = 0;
   while (*str)
     {
       UTF8_SKIP(str);
@@ -23,10 +23,10 @@ utf8_strlen(const byte *str)
   return len;
 }
 
-uint
-utf8_strnlen(const byte *str, uint n)
+size_t
+utf8_strnlen(const byte *str, size_t n)
 {
-  uint len = 0;
+  size_t len = 0;
   const byte *end = str + n;
   while (str < end)
     {
index cc1d5dc65a813d3ee4dea4246e4020f608d1c6e6..55532495004e6e21f3f9f21c1c7394457157b636 100644 (file)
@@ -357,11 +357,11 @@ static inline uint unicode_sanitize_char(uint u)
  * Count the number of Unicode characters in a zero-terminated UTF-8 string.
  * Returned value for corrupted encoding is undefined, but is never greater than strlen().
  **/
-uint utf8_strlen(const byte *str);
+size_t utf8_strlen(const byte *str);
 
 /**
  * Same as @utf8_strlen(), but returns at most @n characters.
  **/
-uint utf8_strnlen(const byte *str, uint n);
+size_t utf8_strnlen(const byte *str, size_t n);
 
 #endif