-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support/mem: add driverkit operations
Within DriverKit we cannot use malloc/free but have to use the DriverKit specific IOMallocZero and IOFree. Signed-off-by: Mads Ynddal <[email protected]> [k.jensen: allowed more generic functions to be retained] Signed-off-by: Klaus Jensen <[email protected]>
- Loading branch information
Showing
4 changed files
with
134 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* SPDX-License-Identifier: LGPL-2.1-or-later or MIT */ | ||
|
||
/* | ||
* This file is part of libvfn. | ||
* | ||
* Copyright (C) 2024 The libvfn Authors. All Rights Reserved. | ||
* | ||
* This library (libvfn) is dual licensed under the GNU Lesser General | ||
* Public License version 2.1 or later or the MIT license. See the | ||
* COPYING and LICENSE files for more information. | ||
*/ | ||
|
||
#ifndef LIBVFN_SUPPORT_PLATFORM_DRIVERKIT_MEM_H | ||
#define LIBVFN_SUPPORT_PLATFORM_DRIVERKIT_MEM_H | ||
|
||
#include <DriverKit/IOLib.h> | ||
#include <DriverKit/IOBufferMemoryDescriptor.h> | ||
|
||
static inline void *xmalloc(size_t sz) | ||
{ | ||
void *mem; | ||
|
||
if (unlikely(!sz)) | ||
return NULL; | ||
|
||
sz += sizeof(uint64_t); | ||
|
||
mem = IOMalloc(sz); | ||
if (unlikely(!mem)) | ||
backtrace_abort(); | ||
|
||
/* prepend length of allocationl (we need it on free) */ | ||
*(uint64_t *)mem = sz; | ||
|
||
return (void *)(((char *)mem) + sizeof(uint64_t)); | ||
} | ||
|
||
static inline void *zmalloc(size_t sz) | ||
{ | ||
void *mem; | ||
|
||
if (unlikely(!sz)) | ||
return NULL; | ||
|
||
sz += sizeof(uint64_t); | ||
|
||
mem = IOMallocZero(sz); | ||
if (unlikely(!mem)) | ||
backtrace_abort(); | ||
|
||
/* prepend length of allocationl (we need it on free) */ | ||
*(uint64_t *)mem = sz; | ||
|
||
return (void *)(((char *)mem) + sizeof(uint64_t)); | ||
} | ||
|
||
static inline void free(void *ptr) | ||
{ | ||
void *real_ptr = (void *)(((uint8_t *)ptr) - sizeof(uint64_t)); | ||
uint64_t len = *(uint64_t *)real_ptr; | ||
|
||
IOFree(real_ptr, len); | ||
} | ||
|
||
#endif /* LIBVFN_SUPPORT_PLATFORM_DRIVERKIT_MEM_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* SPDX-License-Identifier: LGPL-2.1-or-later or MIT */ | ||
|
||
/* | ||
* This file is part of libvfn. | ||
* | ||
* Copyright (C) 2022 The libvfn Authors. All Rights Reserved. | ||
* | ||
* This library (libvfn) is dual licensed under the GNU Lesser General | ||
* Public License version 2.1 or later or the MIT license. See the | ||
* COPYING and LICENSE files for more information. | ||
*/ | ||
|
||
#ifndef LIBVFN_SUPPORT_PLATFORM_POSIX_MEM_H | ||
#define LIBVFN_SUPPORT_PLATFORM_POSIX_MEM_H | ||
|
||
/** | ||
* xmalloc - version of malloc that cannot fail | ||
* @sz: number of bytes to allocate | ||
* | ||
* Call malloc, but only return NULL when @sz is zero. Otherwise, abort. | ||
* | ||
* Return: pointer to allocated memory | ||
*/ | ||
static inline void *xmalloc(size_t sz) | ||
{ | ||
void *mem; | ||
|
||
if (unlikely(!sz)) | ||
return NULL; | ||
|
||
mem = malloc(sz); | ||
if (unlikely(!mem)) | ||
backtrace_abort(); | ||
|
||
return mem; | ||
} | ||
|
||
static inline void *zmalloc(size_t sz) | ||
{ | ||
void *mem; | ||
|
||
if (unlikely(!sz)) | ||
return NULL; | ||
|
||
mem = calloc(1, sz); | ||
if (unlikely(!mem)) | ||
backtrace_abort(); | ||
|
||
return mem; | ||
} | ||
|
||
static inline void *reallocn(void *mem, unsigned int n, size_t sz) | ||
{ | ||
if (would_overflow(n, sz)) { | ||
fprintf(stderr, "allocation of %d * %zu bytes would overflow\n", n, sz); | ||
|
||
backtrace_abort(); | ||
} | ||
|
||
return realloc(mem, n * sz); | ||
} | ||
|
||
#endif /* LIBVFN_SUPPORT_PLATFORM_POSIX_MEM_H */ |