Memory area manipulation


Functions

void * mem_alloc (int n)
 allocate memory
void * mem_ccpy (void *s1, const void *s2, int c, int n)
 copy memory block until character is found
void * mem_chr (const void *s, int c, int n)
 find character in memory block
int mem_cmp (const void *s1, const void *s2, int n)
 compare two memory regions
void * mem_cpy (void *s1, const void *s2, int n)
 copy memory block
void * mem_dup (const void *s, int n)
 duplicate a memory block
void mem_free (void *s)
 free memory
void mem_freeall (void)
 free all memory
int mem_idx (const void *s, int c, int n)
 find character in memory block
void * mem_realloc (void *s, int n)
 reallocate memory
void * mem_set (void *s, int c, int n)
 fill memory block with character


Function Documentation

void* mem_alloc ( int  n  ) 

allocate memory

Parameters:
[in] n allocate n bytes
Returns:
A pointer to newly allocated memory, NULL otherwise.

Definition at line 24 of file mem_alloc.c.

References _mem_pool, _mem_pool_t::list, and mem_set().

Referenced by _lucid_vasprintf(), exec_fork(), exec_fork_background(), exec_fork_pipe(), exec_replace(), log_init(), mem_dup(), mem_realloc(), readsymlink(), str_read(), str_readfile(), str_readline(), stralloc_finalize(), strtok_append(), strtok_init_argv(), and strtok_init_str().

00025 {
00026         if (!_mem_pool) {
00027                 if ((_mem_pool = malloc(sizeof(_mem_pool_t))) == NULL)
00028                         return NULL;
00029 
00030                 INIT_LIST_HEAD(&(_mem_pool->list));
00031         }
00032 
00033         _mem_pool_t *new;
00034 
00035         if ((new = malloc(sizeof(_mem_pool_t))) == NULL)
00036                 return NULL;
00037 
00038         new->len = n;
00039 
00040         if ((new->mem = malloc(new->len)) == NULL) {
00041                 free(new);
00042                 return NULL;
00043         }
00044 
00045         mem_set(new->mem, 0, new->len);
00046 
00047         list_add_tail(&(new->list), &(_mem_pool->list));
00048 
00049         return new->mem;
00050 }

void* mem_ccpy ( void *  s1,
const void *  s2,
int  c,
int  n 
)

copy memory block until character is found

Parameters:
[out] s1 pointer to destination block
[in] s2 pointer to source block
[in] n copy first n bytes of s2
Returns:
A pointer to s1.

Definition at line 19 of file mem_ccpy.c.

00020 {
00021         unsigned char       *a = s1;
00022         const unsigned char *b = s2;
00023 
00024         while (n--) {
00025                 *a++ = *b;
00026 
00027                 if (*b == c)
00028                         return (void *) a;
00029 
00030                 b++;
00031         }
00032 
00033         return 0;
00034 }

void* mem_chr ( const void *  s,
int  c,
int  n 
)

find character in memory block

Parameters:
[in] s pointer to memory block
[in] c character to look for
[in] n scan first n bytes
Returns:
A pointer to the first matching character found, NULL otherwise.

Definition at line 19 of file mem_chr.c.

00020 {
00021         const unsigned char *p = s;
00022 
00023         for (; n--; p++)
00024                 if (*p == c)
00025                         return (void *) p;
00026 
00027         return 0;
00028 }

int mem_cmp ( const void *  s1,
const void *  s2,
int  n 
)

compare two memory regions

Parameters:
[in] s1 pointer to first memory region
[in] s2 pointer to second memory region
[in] n compare n bytes
Returns:
An integer less than, equal to, or greater than zero according to whether s1 is lexicographically less than, equal to, or greater than s2.

Definition at line 19 of file mem_cmp.c.

Referenced by str_path_concat(), and str_str().

00020 {
00021         int d, i;
00022         const unsigned char *a = s1;
00023         const unsigned char *b = s2;
00024 
00025         for (i = 0; i < n; i++)
00026                 if ((d = a[i] - b[i]) != 0)
00027                         return d;
00028 
00029         return 0;
00030 }

void* mem_cpy ( void *  s1,
const void *  s2,
int  n 
)

copy memory block

Parameters:
[out] s1 pointer to destination block
[in] s2 pointer to source block
[in] n copy first n bytes of s2
Returns:
A pointer to s1.

Definition at line 19 of file mem_cpy.c.

Referenced by log_init(), mem_dup(), str_cpy(), str_cpyn(), stralloc_catb(), stralloc_copyb(), stralloc_finalize(), and whirlpool_finalize().

00020 {
00021         unsigned char       *a = s1;
00022         const unsigned char *b = s2;
00023 
00024         while (n--)
00025                 *a++ = *b++;
00026 
00027         return s1;
00028 }

void* mem_dup ( const void *  s,
int  n 
)

duplicate a memory block

Parameters:
[in] s pointer to source memory area
[in] n duplicate first n bytes
Returns:
A pointer to the duplicated memory block, or NULL if insufficient memory was available.

Definition at line 19 of file mem_dup.c.

References mem_alloc(), and mem_cpy().

Referenced by str_dup().

00020 {
00021         void *d = mem_alloc(n);
00022 
00023         if (d)
00024                 return mem_cpy(d, s, n);
00025 
00026         return 0;
00027 }

void mem_free ( void *  s  ) 

free memory

Parameters:
[in] s memory area to free

Definition at line 23 of file mem_free.c.

References _mem_pool, _mem_pool_t::list, _mem_pool_t::mem, and mem_for_each.

Referenced by _lucid_vdprintf(), exec_fork(), exec_fork_background(), exec_fork_pipe(), exec_replace(), ismount(), log_close(), log_traceme(), mem_realloc(), mkdirnamep(), readsymlink(), runlink(), str_path_basename(), str_path_dirname(), str_read(), str_readfile(), str_readline(), stralloc_catf(), stralloc_free(), strtok_append(), strtok_delete(), strtok_free(), and strtok_init_str().

00024 {
00025         int errno_orig = errno;
00026         _mem_pool_t *p;
00027 
00028         mem_for_each(_mem_pool, p)
00029                 if (p->mem == s)
00030                         break;
00031 
00032         if (p->mem != s)
00033                 return;
00034 
00035         list_del(&(p->list));
00036 
00037         free(p->mem);
00038         free(p);
00039 
00040         errno = errno_orig;
00041 }

void mem_freeall ( void   ) 

free all memory

Definition at line 22 of file mem_freeall.c.

References _mem_pool, _mem_pool_t::list, _mem_pool_t::mem, and mem_for_each_safe.

00023 {
00024         _mem_pool_t *p, *tmp;
00025 
00026         if (!_mem_pool)
00027                 return;
00028 
00029         mem_for_each_safe(_mem_pool, p, tmp) {
00030                 list_del(&(p->list));
00031                 free(p->mem);
00032                 free(p);
00033         }
00034 }

int mem_idx ( const void *  s,
int  c,
int  n 
)

find character in memory block

Parameters:
[out] s pointer to memory block
[in] c character to look for
[in] n scan first n bytes
Returns:
An integer offset to the character in the memory block starting at s.
Note:
This function will scan any number of bytes until a NULL character is found if n is less than or equal to zero.

Definition at line 19 of file mem_idx.c.

00020 {
00021         int i;
00022         const unsigned char *p = s;
00023 
00024         for (i = 0; n--, *p++; i++)
00025                 if (c == *p)
00026                         return i;
00027 
00028         return -1;
00029 }

void* mem_realloc ( void *  s,
int  n 
)

reallocate memory

Parameters:
[in] s memory area to reallocate
[in] n allocate n bytes
Returns:
A pointer to newly allocated memory, NULL otherwise.

Definition at line 23 of file mem_realloc.c.

References _mem_pool, _mem_pool_t::len, _mem_pool_t::mem, mem_alloc(), mem_for_each, mem_free(), and mem_set().

Referenced by readsymlink(), str_readfile(), str_readline(), and stralloc_ready().

00024 {
00025         if (!s) {
00026                 if (n > 0)
00027                         return mem_alloc(n);
00028                 else
00029                         return NULL;
00030         }
00031 
00032         else if (n < 1) {
00033                 mem_free(s);
00034                 return NULL;
00035         }
00036 
00037         _mem_pool_t *p;
00038 
00039         mem_for_each(_mem_pool, p)
00040                 if (p->mem == s)
00041                         break;
00042 
00043         if (p->mem != s) {
00044                 errno = EINVAL;
00045                 return NULL;
00046         }
00047 
00048         char *m = realloc(p->mem, n);
00049 
00050         if (!m)
00051                 return NULL;
00052 
00053         p->mem = m;
00054 
00055         if (n > p->len)
00056                 mem_set(m + p->len, 0, n - p->len);
00057 
00058         p->len = n;
00059 
00060         return p->mem;
00061 }

void* mem_set ( void *  s,
int  c,
int  n 
)

fill memory block with character

Parameters:
[out] s pointer to memory block
[in] c character value to be set
[in] n fill first n bytes
Returns:
A pointer to s1.

Definition at line 19 of file mem_set.c.

Referenced by _lucid_vsnprintf(), mem_alloc(), mem_realloc(), strtok_init_str(), tcp_connect(), tcp_listen(), whirlpool_finalize(), and whirlpool_init().

00020 {
00021         unsigned char *p = s;
00022 
00023         while (n--)
00024                 *p++ = c;
00025 
00026         return s;
00027 }


Generated on Tue Jun 19 20:38:45 2007 for lucid by  doxygen 1.5.2