mirror of
https://github.com/eclipse-mosquitto/mosquitto.git
synced 2026-02-05 10:40:05 +08:00
Windows build fixes
This commit is contained in:
@@ -1,48 +1,59 @@
|
||||
set(C_SRC
|
||||
base64_common.c
|
||||
file_common.c
|
||||
memory_common.c
|
||||
mqtt_common.c
|
||||
property_common.c
|
||||
random_common.c
|
||||
strings_common.c
|
||||
time_common.c
|
||||
topic_common.c
|
||||
utf8_common.c
|
||||
)
|
||||
|
||||
if(WIN32)
|
||||
add_library(libmosquitto_common SHARED
|
||||
${C_SRC}
|
||||
)
|
||||
else()
|
||||
add_library(libmosquitto_common OBJECT
|
||||
${C_SRC}
|
||||
)
|
||||
endif()
|
||||
|
||||
target_include_directories(libmosquitto_common
|
||||
PUBLIC
|
||||
"${mosquitto_SOURCE_DIR}/"
|
||||
"${mosquitto_SOURCE_DIR}/include"
|
||||
)
|
||||
|
||||
target_link_libraries(libmosquitto_common
|
||||
PUBLIC
|
||||
config-header
|
||||
)
|
||||
|
||||
if (WITH_TLS)
|
||||
target_link_libraries(libmosquitto_common
|
||||
PUBLIC
|
||||
OpenSSL::SSL
|
||||
)
|
||||
endif()
|
||||
|
||||
|
||||
set_target_properties(libmosquitto_common PROPERTIES
|
||||
OUTPUT_NAME mosquitto_common
|
||||
VERSION ${VERSION}
|
||||
SOVERSION 1
|
||||
POSITION_INDEPENDENT_CODE 1
|
||||
)
|
||||
set(C_SRC
|
||||
base64_common.c
|
||||
file_common.c
|
||||
memory_common.c
|
||||
mqtt_common.c
|
||||
property_common.c
|
||||
random_common.c
|
||||
strings_common.c
|
||||
time_common.c
|
||||
topic_common.c
|
||||
utf8_common.c
|
||||
|
||||
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon.h"
|
||||
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_base64.h"
|
||||
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_file.h"
|
||||
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_memory.h"
|
||||
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_properties.h"
|
||||
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_random.h"
|
||||
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_string.h"
|
||||
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_time.h"
|
||||
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_topic.h"
|
||||
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_utf8.h"
|
||||
)
|
||||
|
||||
if(WIN32)
|
||||
add_library(libmosquitto_common SHARED
|
||||
${C_SRC}
|
||||
)
|
||||
else()
|
||||
add_library(libmosquitto_common OBJECT
|
||||
${C_SRC}
|
||||
)
|
||||
endif()
|
||||
|
||||
target_include_directories(libmosquitto_common
|
||||
PUBLIC
|
||||
"${mosquitto_SOURCE_DIR}/"
|
||||
"${mosquitto_SOURCE_DIR}/include"
|
||||
)
|
||||
|
||||
target_link_libraries(libmosquitto_common
|
||||
PUBLIC
|
||||
config-header
|
||||
)
|
||||
|
||||
if (WITH_TLS)
|
||||
target_link_libraries(libmosquitto_common
|
||||
PUBLIC
|
||||
OpenSSL::SSL
|
||||
)
|
||||
endif()
|
||||
|
||||
|
||||
set_target_properties(libmosquitto_common PROPERTIES
|
||||
OUTPUT_NAME mosquitto_common
|
||||
VERSION ${VERSION}
|
||||
SOVERSION 1
|
||||
POSITION_INDEPENDENT_CODE 1
|
||||
)
|
||||
|
||||
@@ -1,192 +1,198 @@
|
||||
/*
|
||||
Copyright (c) 2009-2021 Roger Light <roger@atchoo.org>
|
||||
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License 2.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
The Eclipse Public License is available at
|
||||
https://www.eclipse.org/legal/epl-2.0/
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
|
||||
|
||||
Contributors:
|
||||
Roger Light - initial implementation and documentation.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "memory_common.h"
|
||||
|
||||
#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
|
||||
|
||||
static size_t mem_limit = 0;
|
||||
void mosquitto_memory_set_limit(size_t lim)
|
||||
{
|
||||
mem_limit = lim;
|
||||
}
|
||||
|
||||
BROKER_EXPORT void *mosquitto_calloc(size_t nmemb, size_t size)
|
||||
{
|
||||
void *mem;
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(mem_limit && memcount + size > mem_limit){
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
mem = calloc(nmemb, size);
|
||||
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(mem){
|
||||
memcount += malloc_usable_size(mem);
|
||||
if(memcount > max_memcount){
|
||||
max_memcount = memcount;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
BROKER_EXPORT void mosquitto_free(void *mem)
|
||||
{
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(!mem){
|
||||
return;
|
||||
}
|
||||
memcount -= malloc_usable_size(mem);
|
||||
#endif
|
||||
free(mem);
|
||||
}
|
||||
|
||||
BROKER_EXPORT void *mosquitto_malloc(size_t size)
|
||||
{
|
||||
void *mem;
|
||||
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(mem_limit && memcount + size > mem_limit){
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
mem = malloc(size);
|
||||
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(mem){
|
||||
memcount += malloc_usable_size(mem);
|
||||
if(memcount > max_memcount){
|
||||
max_memcount = memcount;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
unsigned long mosquitto_memory_used(void)
|
||||
{
|
||||
return memcount;
|
||||
}
|
||||
|
||||
unsigned long mosquitto_max_memory_used(void)
|
||||
{
|
||||
return max_memcount;
|
||||
}
|
||||
#endif
|
||||
|
||||
BROKER_EXPORT void *mosquitto_realloc(void *ptr, size_t size)
|
||||
{
|
||||
void *mem;
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(mem_limit && memcount + size > mem_limit){
|
||||
return NULL;
|
||||
}
|
||||
if(ptr){
|
||||
memcount -= malloc_usable_size(ptr);
|
||||
}
|
||||
#endif
|
||||
mem = realloc(ptr, size);
|
||||
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(mem){
|
||||
memcount += malloc_usable_size(mem);
|
||||
if(memcount > max_memcount){
|
||||
max_memcount = memcount;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
BROKER_EXPORT char *mosquitto_strdup(const char *s)
|
||||
{
|
||||
char *str;
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(mem_limit && memcount + strlen(s) > mem_limit){
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
str = strdup(s);
|
||||
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(str){
|
||||
memcount += malloc_usable_size(str);
|
||||
if(memcount > max_memcount){
|
||||
max_memcount = memcount;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
BROKER_EXPORT char *mosquitto_strndup(const char *s, size_t n)
|
||||
{
|
||||
char *str;
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(mem_limit && memcount + strlen(s) > mem_limit){
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
str = malloc(n+1);
|
||||
if(!str) return NULL;
|
||||
memcpy(str, s, n);
|
||||
str[n] = 0;
|
||||
#else
|
||||
str = strndup(s, n);
|
||||
#endif
|
||||
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(str){
|
||||
memcount += malloc_usable_size(str);
|
||||
if(memcount > max_memcount){
|
||||
max_memcount = memcount;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return str;
|
||||
}
|
||||
/*
|
||||
Copyright (c) 2009-2021 Roger Light <roger@atchoo.org>
|
||||
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License 2.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
The Eclipse Public License is available at
|
||||
https://www.eclipse.org/legal/epl-2.0/
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
|
||||
|
||||
Contributors:
|
||||
Roger Light - initial implementation and documentation.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mosquitto.h"
|
||||
|
||||
#if defined(WITH_MEMORY_TRACKING) && defined(WITH_BROKER)
|
||||
# if defined(__APPLE__) || defined(__FreeBSD__) || defined(__GLIBC__)
|
||||
# define REAL_WITH_MEMORY_TRACKING
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#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
|
||||
|
||||
static size_t mem_limit = 0;
|
||||
void mosquitto_memory_set_limit(size_t lim)
|
||||
{
|
||||
mem_limit = lim;
|
||||
}
|
||||
|
||||
BROKER_EXPORT void *mosquitto_calloc(size_t nmemb, size_t size)
|
||||
{
|
||||
void *mem;
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(mem_limit && memcount + size > mem_limit){
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
mem = calloc(nmemb, size);
|
||||
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(mem){
|
||||
memcount += malloc_usable_size(mem);
|
||||
if(memcount > max_memcount){
|
||||
max_memcount = memcount;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
BROKER_EXPORT void mosquitto_free(void *mem)
|
||||
{
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(!mem){
|
||||
return;
|
||||
}
|
||||
memcount -= malloc_usable_size(mem);
|
||||
#endif
|
||||
free(mem);
|
||||
}
|
||||
|
||||
BROKER_EXPORT void *mosquitto_malloc(size_t size)
|
||||
{
|
||||
void *mem;
|
||||
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(mem_limit && memcount + size > mem_limit){
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
mem = malloc(size);
|
||||
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(mem){
|
||||
memcount += malloc_usable_size(mem);
|
||||
if(memcount > max_memcount){
|
||||
max_memcount = memcount;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
unsigned long mosquitto_memory_used(void)
|
||||
{
|
||||
return memcount;
|
||||
}
|
||||
|
||||
unsigned long mosquitto_max_memory_used(void)
|
||||
{
|
||||
return max_memcount;
|
||||
}
|
||||
#endif
|
||||
|
||||
BROKER_EXPORT void *mosquitto_realloc(void *ptr, size_t size)
|
||||
{
|
||||
void *mem;
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(mem_limit && memcount + size > mem_limit){
|
||||
return NULL;
|
||||
}
|
||||
if(ptr){
|
||||
memcount -= malloc_usable_size(ptr);
|
||||
}
|
||||
#endif
|
||||
mem = realloc(ptr, size);
|
||||
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(mem){
|
||||
memcount += malloc_usable_size(mem);
|
||||
if(memcount > max_memcount){
|
||||
max_memcount = memcount;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
BROKER_EXPORT char *mosquitto_strdup(const char *s)
|
||||
{
|
||||
char *str;
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(mem_limit && memcount + strlen(s) > mem_limit){
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
str = strdup(s);
|
||||
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(str){
|
||||
memcount += malloc_usable_size(str);
|
||||
if(memcount > max_memcount){
|
||||
max_memcount = memcount;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
BROKER_EXPORT char *mosquitto_strndup(const char *s, size_t n)
|
||||
{
|
||||
char *str;
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(mem_limit && memcount + strlen(s) > mem_limit){
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
str = malloc(n+1);
|
||||
if(!str) return NULL;
|
||||
memcpy(str, s, n);
|
||||
str[n] = 0;
|
||||
#else
|
||||
str = strndup(s, n);
|
||||
#endif
|
||||
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
if(str){
|
||||
memcount += malloc_usable_size(str);
|
||||
if(memcount > max_memcount){
|
||||
max_memcount = memcount;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2010-2021 Roger Light <roger@atchoo.org>
|
||||
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License 2.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
The Eclipse Public License is available at
|
||||
https://www.eclipse.org/legal/epl-2.0/
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
|
||||
|
||||
Contributors:
|
||||
Roger Light - initial implementation and documentation.
|
||||
*/
|
||||
|
||||
#ifndef MEMORY_MOSQ_H
|
||||
#define MEMORY_MOSQ_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#if defined(WITH_MEMORY_TRACKING) && defined(WITH_BROKER)
|
||||
# if defined(__APPLE__) || defined(__FreeBSD__) || defined(__GLIBC__)
|
||||
# define REAL_WITH_MEMORY_TRACKING
|
||||
# endif
|
||||
#endif
|
||||
|
||||
void *mosquitto__calloc(size_t nmemb, size_t size);
|
||||
void mosquitto_free(void *mem);
|
||||
void *mosquitto_malloc(size_t size);
|
||||
#ifdef REAL_WITH_MEMORY_TRACKING
|
||||
unsigned long mosquitto__memory_used(void);
|
||||
unsigned long mosquitto__max_memory_used(void);
|
||||
#endif
|
||||
void *mosquitto_realloc(void *ptr, size_t size);
|
||||
char *mosquitto_strdup(const char *s);
|
||||
char *mosquitto__strndup(const char *s, size_t n);
|
||||
|
||||
#ifdef WITH_BROKER
|
||||
void memory__set_limit(size_t lim);
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
@@ -1,39 +1,39 @@
|
||||
/*
|
||||
Copyright (c) 2009-2021 Roger Light <roger@atchoo.org>
|
||||
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License 2.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
The Eclipse Public License is available at
|
||||
https://www.eclipse.org/legal/epl-2.0/
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
|
||||
|
||||
Contributors:
|
||||
Roger Light - initial implementation and documentation.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "mosquitto/mqtt_protocol.h"
|
||||
|
||||
|
||||
unsigned int mosquitto_varint_bytes(uint32_t word)
|
||||
{
|
||||
if(word < 128){
|
||||
return 1;
|
||||
}else if(word < 16384){
|
||||
return 2;
|
||||
}else if(word < 2097152){
|
||||
return 3;
|
||||
}else if(word < 268435456){
|
||||
return 4;
|
||||
}else{
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
/*
|
||||
Copyright (c) 2009-2021 Roger Light <roger@atchoo.org>
|
||||
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License 2.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
The Eclipse Public License is available at
|
||||
https://www.eclipse.org/legal/epl-2.0/
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
|
||||
|
||||
Contributors:
|
||||
Roger Light - initial implementation and documentation.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "mosquitto.h"
|
||||
|
||||
|
||||
unsigned int mosquitto_varint_bytes(uint32_t word)
|
||||
{
|
||||
if(word < 128){
|
||||
return 1;
|
||||
}else if(word < 16384){
|
||||
return 2;
|
||||
}else if(word < 2097152){
|
||||
return 3;
|
||||
}else if(word < 268435456){
|
||||
return 4;
|
||||
}else{
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user