2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* netutils/thttpd/thttpd_alloc.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
|
2012-09-13 18:58:49 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "thttpd_alloc.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_THTTPD
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef MAX
|
|
|
|
# define MAX(a,b) ((a) > (b) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
# define MIN(a,b) ((a) < (b) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_THTTPD_MEMDEBUG
|
|
|
|
static int g_nallocations = 0;
|
|
|
|
static int g_nfreed = 0;
|
|
|
|
static size_t g_allocated = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Generate debugging statistics */
|
|
|
|
|
|
|
|
#ifdef CONFIG_THTTPD_MEMDEBUG
|
|
|
|
void httpd_memstats(void)
|
|
|
|
{
|
|
|
|
static struct mallinfo mm;
|
|
|
|
|
2016-06-15 01:53:47 +02:00
|
|
|
ninfo("%d allocations (%lu bytes), %d freed\n",
|
|
|
|
g_nallocations, (unsigned long)g_allocated, g_nfreed);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Get the current memory usage */
|
|
|
|
|
|
|
|
#ifdef CONFIG_CAN_PASS_STRUCTS
|
|
|
|
mm = mallinfo();
|
|
|
|
#else
|
|
|
|
(void)mallinfo(&mm);
|
|
|
|
#endif
|
2016-06-15 01:53:47 +02:00
|
|
|
ninfo("arena: %08x ordblks: %08x mxordblk: %08x uordblks: %08x fordblks: %08x\n",
|
2011-03-19 22:04:13 +01:00
|
|
|
mm.arena, mm.ordblks, mm.mxordblk, mm.uordblks, mm.fordblks);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-03 00:20:33 +02:00
|
|
|
* Public Functions
|
2011-03-19 22:04:13 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_THTTPD_MEMDEBUG
|
|
|
|
FAR void *httpd_malloc(size_t nbytes)
|
|
|
|
{
|
|
|
|
void *ptr = malloc(nbytes);
|
|
|
|
if (!ptr)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Allocation of %d bytes failed\n", nbytes);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Allocated %d bytes at %p\n", nbytes, ptr);
|
2011-03-19 22:04:13 +01:00
|
|
|
g_nallocations++;
|
|
|
|
g_allocated += nbytes;
|
|
|
|
}
|
|
|
|
httpd_memstats();
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_THTTPD_MEMDEBUG
|
|
|
|
FAR void *httpd_realloc(FAR void *oldptr, size_t oldsize, size_t newsize)
|
|
|
|
{
|
|
|
|
void *ptr = realloc(oldptr, newsize);
|
|
|
|
if (!ptr)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Re-allocation from %d to %d bytes failed\n",
|
2011-03-19 22:04:13 +01:00
|
|
|
oldsize, newsize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Re-allocated form %d to %d bytes (from %p to %p)\n",
|
2011-03-19 22:04:13 +01:00
|
|
|
oldsize, newsize, oldptr, ptr);
|
|
|
|
g_allocated += (newsize - oldsize);
|
|
|
|
}
|
|
|
|
httpd_memstats();
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_THTTPD_MEMDEBUG
|
|
|
|
void httpd_free(FAR void *ptr)
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
g_nfreed++;
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Freed memory at %p\n", ptr);
|
2011-03-19 22:04:13 +01:00
|
|
|
httpd_memstats();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_THTTPD_MEMDEBUG
|
|
|
|
FAR char *httpd_strdup(const char *str)
|
|
|
|
{
|
|
|
|
FAR char *newstr = strdup(str);
|
|
|
|
if (!newstr)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: strdup of %s failed\n", str);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("strdup'ed %s\n", str);
|
2011-03-19 22:04:13 +01:00
|
|
|
g_nallocations++;
|
|
|
|
g_allocated += (strlen(str)+1);
|
|
|
|
}
|
|
|
|
httpd_memstats();
|
|
|
|
return newstr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Helpers to implement dynamically allocated strings */
|
|
|
|
|
|
|
|
void httpd_realloc_str(char **pstr, size_t *maxsize, size_t size)
|
|
|
|
{
|
|
|
|
size_t oldsize;
|
|
|
|
if (*maxsize == 0)
|
|
|
|
{
|
|
|
|
*maxsize = MAX(CONFIG_THTTPD_MINSTRSIZE, size + CONFIG_THTTPD_REALLOCINCR);
|
|
|
|
*pstr = NEW(char, *maxsize + 1);
|
|
|
|
}
|
|
|
|
else if (size > *maxsize)
|
|
|
|
{
|
|
|
|
oldsize = *maxsize;
|
|
|
|
*maxsize = MAX(oldsize * 2, size * 5 / 4);
|
|
|
|
*pstr = httpd_realloc(*pstr, oldsize + 1, *maxsize + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*pstr)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: out of memory reallocating a string to %d bytes\n",
|
|
|
|
*maxsize);
|
2011-03-19 22:04:13 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_THTTPD */
|