2014-05-07 22:27:00 +00:00
|
|
|
/*
|
2020-02-27 23:26:58 +00:00
|
|
|
Copyright (c) 2009-2020 Roger Light <roger@atchoo.org>
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
All rights reserved. This program and the accompanying materials
|
2020-11-25 17:34:21 +00:00
|
|
|
are made available under the terms of the Eclipse Public License 2.0
|
2014-05-07 22:27:00 +00:00
|
|
|
and Eclipse Distribution License v1.0 which accompany this distribution.
|
2021-10-05 14:20:37 +00:00
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
The Eclipse Public License is available at
|
2020-11-25 17:34:21 +00:00
|
|
|
https://www.eclipse.org/legal/epl-2.0/
|
2014-05-07 22:27:00 +00:00
|
|
|
and the Eclipse Distribution License is available at
|
|
|
|
http://www.eclipse.org/org/documents/edl-v10.php.
|
2021-10-05 14:20:37 +00:00
|
|
|
|
2021-01-20 11:46:18 +00:00
|
|
|
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
|
2020-12-01 18:21:59 +00:00
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
Contributors:
|
|
|
|
Roger Light - initial implementation and documentation.
|
|
|
|
*/
|
|
|
|
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "config.h"
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "memory_mosq.h"
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
#ifdef REAL_WITH_MEMORY_TRACKING
|
|
|
|
# if defined(__APPLE__)
|
|
|
|
# include <malloc/malloc.h>
|
|
|
|
# define malloc_usable_size malloc_size
|
|
|
|
# elif defined(__FreeBSD__)
|
|
|
|
# include <malloc_np.h>
|
|
|
|
# else
|
|
|
|
# include <malloc.h>
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef REAL_WITH_MEMORY_TRACKING
|
|
|
|
static unsigned long memcount = 0;
|
|
|
|
static unsigned long max_memcount = 0;
|
|
|
|
#endif
|
|
|
|
|
2018-02-24 22:09:19 +00:00
|
|
|
#ifdef WITH_BROKER
|
|
|
|
static size_t mem_limit = 0;
|
|
|
|
void memory__set_limit(size_t lim)
|
|
|
|
{
|
|
|
|
mem_limit = lim;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
void *mosquitto__calloc(size_t nmemb, size_t size)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2020-03-12 10:29:11 +00:00
|
|
|
void *mem;
|
2018-02-24 22:09:19 +00:00
|
|
|
#ifdef REAL_WITH_MEMORY_TRACKING
|
|
|
|
if(mem_limit && memcount + size > mem_limit){
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2020-03-12 10:29:11 +00:00
|
|
|
mem = calloc(nmemb, size);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
#ifdef REAL_WITH_MEMORY_TRACKING
|
2015-09-22 13:42:56 +00:00
|
|
|
if(mem){
|
|
|
|
memcount += malloc_usable_size(mem);
|
|
|
|
if(memcount > max_memcount){
|
|
|
|
max_memcount = memcount;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
void mosquitto__free(void *mem)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
#ifdef REAL_WITH_MEMORY_TRACKING
|
2015-09-16 20:20:40 +00:00
|
|
|
if(!mem){
|
|
|
|
return;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
memcount -= malloc_usable_size(mem);
|
|
|
|
#endif
|
|
|
|
free(mem);
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
void *mosquitto__malloc(size_t size)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2020-03-12 10:29:11 +00:00
|
|
|
void *mem;
|
|
|
|
|
2018-02-24 22:09:19 +00:00
|
|
|
#ifdef REAL_WITH_MEMORY_TRACKING
|
|
|
|
if(mem_limit && memcount + size > mem_limit){
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2020-03-12 10:29:11 +00:00
|
|
|
|
|
|
|
mem = malloc(size);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
#ifdef REAL_WITH_MEMORY_TRACKING
|
2015-09-22 13:42:56 +00:00
|
|
|
if(mem){
|
|
|
|
memcount += malloc_usable_size(mem);
|
|
|
|
if(memcount > max_memcount){
|
|
|
|
max_memcount = memcount;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef REAL_WITH_MEMORY_TRACKING
|
2015-04-19 21:10:59 +00:00
|
|
|
unsigned long mosquitto__memory_used(void)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
return memcount;
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
unsigned long mosquitto__max_memory_used(void)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
return max_memcount;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
void *mosquitto__realloc(void *ptr, size_t size)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2020-03-12 10:29:11 +00:00
|
|
|
void *mem;
|
2018-02-24 22:09:19 +00:00
|
|
|
#ifdef REAL_WITH_MEMORY_TRACKING
|
|
|
|
if(mem_limit && memcount + size > mem_limit){
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
if(ptr){
|
|
|
|
memcount -= malloc_usable_size(ptr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
mem = realloc(ptr, size);
|
|
|
|
|
|
|
|
#ifdef REAL_WITH_MEMORY_TRACKING
|
2015-09-22 13:42:56 +00:00
|
|
|
if(mem){
|
|
|
|
memcount += malloc_usable_size(mem);
|
|
|
|
if(memcount > max_memcount){
|
|
|
|
max_memcount = memcount;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
char *mosquitto__strdup(const char *s)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2020-03-12 10:29:11 +00:00
|
|
|
char *str;
|
2018-02-24 22:09:19 +00:00
|
|
|
#ifdef REAL_WITH_MEMORY_TRACKING
|
|
|
|
if(mem_limit && memcount + strlen(s) > mem_limit){
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2020-03-12 10:29:11 +00:00
|
|
|
str = strdup(s);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
#ifdef REAL_WITH_MEMORY_TRACKING
|
2015-09-22 13:42:56 +00:00
|
|
|
if(str){
|
|
|
|
memcount += malloc_usable_size(str);
|
|
|
|
if(memcount > max_memcount){
|
|
|
|
max_memcount = memcount;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|