Skip to content

Commit

Permalink
nvme: Introduce nvme_realloc function
Browse files Browse the repository at this point in the history
This attempts to preserve the functionality of realloc while also
ensuring the final memory is aligned.

Signed-off-by: Brandon Paupore <[email protected]>
  • Loading branch information
bpaupore-wdc committed Sep 22, 2023
1 parent 133db4f commit 5a57078
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions nvme.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
#include "fabrics.h"
#define CREATE_CMD
#include "nvme-builtin.h"
#include "malloc.h"

struct feat_cfg {
enum nvme_features_id feature_id;
Expand Down Expand Up @@ -198,6 +199,20 @@ static void *nvme_alloc(size_t len)
return p;
}

void *nvme_realloc(void *p, size_t len)
{
size_t old_len = malloc_usable_size(p);

void *result = nvme_alloc(len);

if (p) {
memcpy(result, p, min(old_len, len));
free(p);
}

return result;
}

static void *__nvme_alloc_huge(size_t len, bool *huge)
{
void *p;
Expand Down Expand Up @@ -249,6 +264,21 @@ void *nvme_alloc_huge(size_t len, bool *huge)
}
#endif

void *nvme_realloc_huge(void *p, size_t len, bool *huge)
{
size_t old_len = malloc_usable_size(p);
bool was_huge = *huge;

void *result = nvme_alloc_huge(len, huge);

if (p) {
memcpy(result, p, min(old_len, len));
nvme_free_huge(p, was_huge);
}

return result;
}

const char *nvme_strerror(int errnum)
{
if (errnum >= ENVME_CONNECT_RESOLVE)
Expand Down

0 comments on commit 5a57078

Please sign in to comment.