4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "ocf/ocf_def.h"
36 #include "spdk/crc32.h"
38 #include "spdk_internal/log.h"
40 /* Number of buffers for mempool
41 * Need to be power of two - 1 for better memory utilization
42 * It depends on memory usage of OCF which
43 * in itself depends on the workload
44 * It is a big number because OCF uses allocators
45 * for every request it sends and recieves
47 #define ENV_ALLOCATOR_NBUFS 32767
49 /* Use unique index for env allocators */
50 static env_atomic g_env_allocator_index
= 0;
53 env_allocator_new(env_allocator
*allocator
)
55 void *mem
= spdk_mempool_get(allocator
->mempool
);
57 if (spdk_likely(mem
)) {
58 memset(mem
, 0, allocator
->element_size
);
65 env_allocator_create(uint32_t size
, const char *name
)
67 env_allocator
*allocator
;
68 char qualified_name
[128] = {0};
70 snprintf(qualified_name
, 128, "ocf_env_%d", env_atomic_inc_return(&g_env_allocator_index
));
72 allocator
= calloc(1, sizeof(*allocator
));
77 allocator
->mempool
= spdk_mempool_create(qualified_name
,
78 ENV_ALLOCATOR_NBUFS
, size
,
79 SPDK_MEMPOOL_DEFAULT_CACHE_SIZE
,
80 SPDK_ENV_SOCKET_ID_ANY
);
82 if (!allocator
->mempool
) {
87 allocator
->element_size
= size
;
93 env_allocator_del(env_allocator
*allocator
, void *item
)
95 spdk_mempool_put(allocator
->mempool
, item
);
99 env_allocator_destroy(env_allocator
*allocator
)
102 if (ENV_ALLOCATOR_NBUFS
- spdk_mempool_count(allocator
->mempool
)) {
103 SPDK_ERRLOG("Not all objects deallocated\n");
107 spdk_mempool_free(allocator
->mempool
);
112 /* *** COMPLETION *** */
115 env_completion_init(env_completion
*completion
)
117 atomic_set(&completion
->atom
, 1);
121 env_completion_wait(env_completion
*completion
)
123 while (atomic_read(&completion
->atom
)) {
129 env_completion_complete(env_completion
*completion
)
131 atomic_set(&completion
->atom
, 0);
137 env_crc32(uint32_t crc
, uint8_t const *message
, size_t len
)
139 return spdk_crc32_ieee_update(message
, len
, crc
);