Skip to content

Commit

Permalink
env: Move malloc/free wrappers into env
Browse files Browse the repository at this point in the history
Change-Id: Ief591f5e23c4ae06cb77fab647a7afd082450a73
Signed-off-by: Ben Walker <[email protected]>
  • Loading branch information
Ben Walker committed Oct 5, 2016
1 parent a4747c6 commit b9fbdd1
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 41 deletions.
15 changes: 15 additions & 0 deletions include/spdk/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,23 @@
extern "C" {
#endif

#include <stddef.h>
#include <stdint.h>

/**
* Allocate a pinned, physically contiguous memory buffer with the
* given size and alignment. The buffer will be zeroed.
*/
void *
spdk_zmalloc(size_t size, size_t align, uint64_t *phys_addr);

/**
* Free a memory buffer previously allocated with spdk_malloc.
* This call is never made from the performance path.
*/
void
spdk_free(void *buf);

#define SPDK_VTOPHYS_ERROR (0xFFFFFFFFFFFFFFFFULL)

uint64_t spdk_vtophys(void *buf);
Expand Down
2 changes: 1 addition & 1 deletion lib/env/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk

CFLAGS += $(DPDK_INC)
C_SRCS = vtophys.c
C_SRCS = env.c vtophys.c
LIBNAME = env

include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk
56 changes: 56 additions & 0 deletions lib/env/env.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*-
* BSD LICENSE
*
* Copyright (c) Intel Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "spdk/env.h"

#include <string.h>

#include <rte_config.h>
#include <rte_malloc.h>

void *
spdk_zmalloc(size_t size, size_t align, uint64_t *phys_addr)
{
void *buf = rte_malloc(NULL, size, align);
if (buf) {
memset(buf, 0, size);
*phys_addr = rte_malloc_virt2phy(buf);
}
return buf;
}

void
spdk_free(void *buf)
{
return rte_free(buf);
}
4 changes: 2 additions & 2 deletions lib/ioat/ioat.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ ioat_channel_start(struct spdk_ioat_chan *ioat)
ioat->max_xfer_size = 1U << xfercap;
}

ioat->comp_update = ioat_zmalloc(NULL, sizeof(*ioat->comp_update), SPDK_IOAT_CHANCMP_ALIGN,
ioat->comp_update = ioat_zmalloc(sizeof(*ioat->comp_update), SPDK_IOAT_CHANCMP_ALIGN,
&comp_update_bus_addr);
if (ioat->comp_update == NULL) {
return -1;
Expand All @@ -417,7 +417,7 @@ ioat_channel_start(struct spdk_ioat_chan *ioat)
return -1;
}

ioat->hw_ring = ioat_zmalloc(NULL, num_descriptors * sizeof(union spdk_ioat_hw_desc), 64,
ioat->hw_ring = ioat_zmalloc(num_descriptors * sizeof(union spdk_ioat_hw_desc), 64,
&ioat->hw_ring_phys_addr);
if (!ioat->hw_ring) {
return -1;
Expand Down
14 changes: 2 additions & 12 deletions lib/ioat/ioat_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,12 @@
* Allocate a pinned, physically contiguous memory buffer with the
* given size and alignment.
*/
static inline void *
ioat_zmalloc(const char *tag, size_t size, unsigned align, uint64_t *phys_addr)
{
void *buf = rte_malloc(tag, size, align);

if (buf) {
memset(buf, 0, size);
*phys_addr = rte_malloc_virt2phy(buf);
}
return buf;
}
#define ioat_zmalloc spdk_zmalloc

/**
* Free a memory buffer previously allocated with ioat_zmalloc.
*/
#define ioat_free(buf) rte_free(buf)
#define ioat_free spdk_free

/**
* Return the physical address for the specified virtual address.
Expand Down
4 changes: 2 additions & 2 deletions lib/nvme/nvme.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ nvme_attach(void *devhandle)
int status;
uint64_t phys_addr = 0;

ctrlr = nvme_malloc("nvme_ctrlr", sizeof(struct spdk_nvme_ctrlr),
ctrlr = nvme_malloc(sizeof(struct spdk_nvme_ctrlr),
64, &phys_addr);
if (ctrlr == NULL) {
SPDK_ERRLOG("could not allocate ctrlr\n");
Expand Down Expand Up @@ -178,7 +178,7 @@ nvme_allocate_request_user_copy(void *buffer, uint32_t payload_size, spdk_nvme_c
uint64_t phys_addr;

if (buffer && payload_size) {
contig_buffer = nvme_malloc("nvme_user_copy", payload_size, 4096, &phys_addr);
contig_buffer = nvme_malloc(payload_size, 4096, &phys_addr);
if (!contig_buffer) {
return NULL;
}
Expand Down
9 changes: 3 additions & 6 deletions lib/nvme/nvme_ctrlr.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,7 @@ static int nvme_ctrlr_set_intel_support_log_pages(struct spdk_nvme_ctrlr *ctrlr)
struct nvme_completion_poll_status status;
struct spdk_nvme_intel_log_page_directory *log_page_directory;

log_page_directory = nvme_malloc("nvme_log_page_directory",
sizeof(struct spdk_nvme_intel_log_page_directory),
log_page_directory = nvme_malloc(sizeof(struct spdk_nvme_intel_log_page_directory),
64, &phys_addr);
if (log_page_directory == NULL) {
SPDK_ERRLOG("could not allocate log_page_directory\n");
Expand Down Expand Up @@ -375,8 +374,7 @@ nvme_ctrlr_construct_io_qpairs(struct spdk_nvme_ctrlr *ctrlr)
*/
num_trackers = nvme_min(NVME_IO_TRACKERS, (num_entries - 1));

ctrlr->ioq = nvme_malloc("nvme_ioq",
ctrlr->opts.num_io_queues * sizeof(struct spdk_nvme_qpair),
ctrlr->ioq = nvme_malloc(ctrlr->opts.num_io_queues * sizeof(struct spdk_nvme_qpair),
64, &phys_addr);

if (ctrlr->ioq == NULL)
Expand Down Expand Up @@ -695,8 +693,7 @@ nvme_ctrlr_construct_namespaces(struct spdk_nvme_ctrlr *ctrlr)
goto fail;
}

ctrlr->nsdata = nvme_malloc("nvme_namespaces",
nn * sizeof(struct spdk_nvme_ns_data), 64,
ctrlr->nsdata = nvme_malloc(nn * sizeof(struct spdk_nvme_ns_data), 64,
&phys_addr);
if (ctrlr->nsdata == NULL) {
goto fail;
Expand Down
13 changes: 2 additions & 11 deletions lib/nvme/nvme_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,12 @@
* given size and alignment.
* Note: these calls are only made during driver initialization.
*/
static inline void *
nvme_malloc(const char *tag, size_t size, unsigned align, uint64_t *phys_addr)
{
void *buf = rte_malloc(tag, size, align);
if (buf) {
memset(buf, 0, size);
*phys_addr = rte_malloc_virt2phy(buf);
}
return buf;
}
#define nvme_malloc spdk_zmalloc

/**
* Free a memory buffer previously allocated with nvme_malloc.
*/
#define nvme_free(buf) rte_free(buf)
#define nvme_free spdk_free

/**
* Reserve a named, process shared memory zone with the given size,
Expand Down
8 changes: 3 additions & 5 deletions lib/nvme/nvme_qpair.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,7 @@ nvme_qpair_construct(struct spdk_nvme_qpair *qpair, uint16_t id,
}
}
if (qpair->sq_in_cmb == false) {
qpair->cmd = nvme_malloc("qpair_cmd",
qpair->num_entries * sizeof(struct spdk_nvme_cmd),
qpair->cmd = nvme_malloc(qpair->num_entries * sizeof(struct spdk_nvme_cmd),
0x1000,
&qpair->cmd_bus_addr);
if (qpair->cmd == NULL) {
Expand All @@ -566,8 +565,7 @@ nvme_qpair_construct(struct spdk_nvme_qpair *qpair, uint16_t id,
}
}

qpair->cpl = nvme_malloc("qpair_cpl",
qpair->num_entries * sizeof(struct spdk_nvme_cpl),
qpair->cpl = nvme_malloc(qpair->num_entries * sizeof(struct spdk_nvme_cpl),
0x1000,
&qpair->cpl_bus_addr);
if (qpair->cpl == NULL) {
Expand All @@ -589,7 +587,7 @@ nvme_qpair_construct(struct spdk_nvme_qpair *qpair, uint16_t id,
* This ensures the PRP list embedded in the nvme_tracker object will not span a
* 4KB boundary, while allowing access to trackers in tr[] via normal array indexing.
*/
qpair->tr = nvme_malloc("nvme_tr", num_trackers * sizeof(*tr), sizeof(*tr), &phys_addr);
qpair->tr = nvme_malloc(num_trackers * sizeof(*tr), sizeof(*tr), &phys_addr);
if (qpair->tr == NULL) {
SPDK_ERRLOG("nvme_tr failed\n");
goto fail;
Expand Down
2 changes: 1 addition & 1 deletion test/lib/ioat/unit/ioat_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
struct spdk_pci_device;

static inline void *
ioat_zmalloc(const char *tag, size_t size, unsigned align, uint64_t *phys_addr)
ioat_zmalloc(size_t size, unsigned align, uint64_t *phys_addr)
{
return calloc(1, size);
}
Expand Down
2 changes: 1 addition & 1 deletion test/lib/nvme/unit/nvme_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
struct spdk_pci_device;

static inline void *
nvme_malloc(const char *tag, size_t size, unsigned align, uint64_t *phys_addr)
nvme_malloc(size_t size, unsigned align, uint64_t *phys_addr)
{
void *buf = NULL;
if (posix_memalign(&buf, align, size)) {
Expand Down

0 comments on commit b9fbdd1

Please sign in to comment.