Changeset View
Changeset View
Standalone View
Standalone View
contrib/zstd/lib/common/zstd_common.c
/* | /* | ||||
* Copyright (c) 2016-present, Yann Collet, Facebook, Inc. | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. | ||||
* All rights reserved. | * All rights reserved. | ||||
* | * | ||||
* This source code is licensed under both the BSD-style license (found in the | * This source code is licensed under both the BSD-style license (found in the | ||||
* LICENSE file in the root directory of this source tree) and the GPLv2 (found | * LICENSE file in the root directory of this source tree) and the GPLv2 (found | ||||
* in the COPYING file in the root directory of this source tree). | * in the COPYING file in the root directory of this source tree). | ||||
*/ | */ | ||||
/*-************************************* | /*-************************************* | ||||
* Dependencies | * Dependencies | ||||
***************************************/ | ***************************************/ | ||||
#ifdef _KERNEL | |||||
#include "zstd_kmalloc.h" /* malloc, calloc, free */ | |||||
#include <sys/systm.h> /* memset */ | |||||
#else | |||||
#ifdef _BOOTSTRAP | |||||
#include <stand.h> | |||||
#else | |||||
#include <stdlib.h> /* malloc, calloc, free */ | #include <stdlib.h> /* malloc, calloc, free */ | ||||
#include <string.h> /* memset */ | #include <string.h> /* memset */ | ||||
#endif | |||||
#endif | |||||
#include "error_private.h" | #include "error_private.h" | ||||
#define ZSTD_STATIC_LINKING_ONLY | #define ZSTD_STATIC_LINKING_ONLY | ||||
#include "zstd.h" | #include "zstd.h" | ||||
/*-**************************************** | /*-**************************************** | ||||
* Version | * Version | ||||
******************************************/ | ******************************************/ | ||||
Show All 24 Lines | |||||
/*=************************************************************** | /*=************************************************************** | ||||
* Custom allocator | * Custom allocator | ||||
****************************************************************/ | ****************************************************************/ | ||||
void* ZSTD_malloc(size_t size, ZSTD_customMem customMem) | void* ZSTD_malloc(size_t size, ZSTD_customMem customMem) | ||||
{ | { | ||||
if (customMem.customAlloc) | if (customMem.customAlloc) | ||||
return customMem.customAlloc(customMem.opaque, size); | return customMem.customAlloc(customMem.opaque, size); | ||||
#ifdef _KERNEL | |||||
printf("ZSTD:%s:%u: malloc(%zu)\n", __func__, __LINE__, size); | |||||
return malloc(size, M_ZSTD, M_WAITOK); | |||||
#else | |||||
return malloc(size); | return malloc(size); | ||||
#endif | |||||
} | } | ||||
void* ZSTD_calloc(size_t size, ZSTD_customMem customMem) | void* ZSTD_calloc(size_t size, ZSTD_customMem customMem) | ||||
{ | { | ||||
if (customMem.customAlloc) { | if (customMem.customAlloc) { | ||||
/* calloc implemented as malloc+memset; | /* calloc implemented as malloc+memset; | ||||
* not as efficient as calloc, but next best guess for custom malloc */ | * not as efficient as calloc, but next best guess for custom malloc */ | ||||
void* const ptr = customMem.customAlloc(customMem.opaque, size); | void* const ptr = customMem.customAlloc(customMem.opaque, size); | ||||
memset(ptr, 0, size); | memset(ptr, 0, size); | ||||
return ptr; | return ptr; | ||||
} | } | ||||
#ifdef _KERNEL | |||||
printf("ZSTD:%s:%u: malloc(%zu)\n", __func__, __LINE__, size); | |||||
return malloc(size, M_ZSTD, M_WAITOK | M_ZERO); | |||||
#else | |||||
return calloc(1, size); | return calloc(1, size); | ||||
#endif | |||||
} | } | ||||
void ZSTD_free(void* ptr, ZSTD_customMem customMem) | void ZSTD_free(void* ptr, ZSTD_customMem customMem) | ||||
{ | { | ||||
if (ptr!=NULL) { | if (ptr!=NULL) { | ||||
if (customMem.customFree) | if (customMem.customFree) | ||||
customMem.customFree(customMem.opaque, ptr); | customMem.customFree(customMem.opaque, ptr); | ||||
else | else | ||||
#ifdef _KERNEL | |||||
free(ptr, M_ZSTD); | |||||
#else | |||||
free(ptr); | free(ptr); | ||||
#endif | |||||
} | } | ||||
} | } |