2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* netutils/thttpd/thttpd_alloc.c
|
|
|
|
*
|
2020-04-11 20:13:56 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
2020-04-11 20:13:56 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
2020-04-11 20:13:56 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
2020-06-15 08:02:49 +02:00
|
|
|
#include <malloc.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
#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 */
|
|
|
|
|
|
|
|
mm = mallinfo();
|
2020-04-11 20:13:56 +02:00
|
|
|
|
2020-04-11 20:56:24 +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;
|
|
|
|
}
|
2020-04-11 20:56:24 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
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);
|
|
|
|
}
|
2020-04-11 20:56:24 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
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);
|
|
|
|
}
|
2020-04-11 20:56:24 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
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)
|
|
|
|
{
|
2020-04-11 20:56:24 +02:00
|
|
|
*maxsize = MAX(CONFIG_THTTPD_MINSTRSIZE,
|
|
|
|
size + CONFIG_THTTPD_REALLOCINCR);
|
2011-03-19 22:04:13 +01:00
|
|
|
*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 */
|