Commit 6b583394 authored by Sergey Vojtovich's avatar Sergey Vojtovich

Fixup: 32bit n_caches and magic

In reply to:
Why is n_caches uint64?  When reading the code, I would assume that this means it's very big.  Why not just uint ?
I can understand you want the header structure aligned, but that is independent of the user interface that is using 'n'
alos, please replace 'n' as a parameter to cache_slot or something more readable
Another thing to think about is to replace argument checking to use assert instead of return...
if (n >= dir->header->n_caches)
   return -1;
parent 8981c2da
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
/* PMAC0\0\0\0 */ /* PMAC0\0\0\0 */
static const uint64_t pmem_append_cache_magic= 0x00000000010dfefe; static const uint32_t pmem_append_cache_magic= 0x010dfefe;
/** /**
...@@ -42,7 +42,7 @@ static const uint64_t pmem_append_cache_magic= 0x00000000010dfefe; ...@@ -42,7 +42,7 @@ static const uint64_t pmem_append_cache_magic= 0x00000000010dfefe;
@return full directory header size @return full directory header size
*/ */
static uint64_t directory_header_size(uint64_t n_caches) static uint64_t directory_header_size(uint32_t n_caches)
{ {
return sizeof(PMEM_APPEND_CACHE_DIRECTORY_HEADER) + return sizeof(PMEM_APPEND_CACHE_DIRECTORY_HEADER) +
sizeof(uint64_t) * n_caches; sizeof(uint64_t) * n_caches;
...@@ -73,7 +73,7 @@ static uint64_t directory_header_size(uint64_t n_caches) ...@@ -73,7 +73,7 @@ static uint64_t directory_header_size(uint64_t n_caches)
*/ */
static int create_directory(PMEM_APPEND_CACHE_DIRECTORY *dir, static int create_directory(PMEM_APPEND_CACHE_DIRECTORY *dir,
const char *path, uint64_t size, uint64_t n_caches) const char *path, uint64_t size, uint32_t n_caches)
{ {
const uint64_t header_size= directory_header_size(n_caches); const uint64_t header_size= directory_header_size(n_caches);
uint64_t start_offset= header_size; uint64_t start_offset= header_size;
...@@ -96,7 +96,7 @@ static int create_directory(PMEM_APPEND_CACHE_DIRECTORY *dir, ...@@ -96,7 +96,7 @@ static int create_directory(PMEM_APPEND_CACHE_DIRECTORY *dir,
return -1; return -1;
dir->start_offsets= (void*) (dir->header + 1); dir->start_offsets= (void*) (dir->header + 1);
for (uint64_t i= 0; i < n_caches; i++) for (uint32_t i= 0; i < n_caches; i++)
{ {
dir->start_offsets[i]= start_offset; dir->start_offsets[i]= start_offset;
start_offset+= cache_size; start_offset+= cache_size;
...@@ -116,7 +116,7 @@ static int create_directory(PMEM_APPEND_CACHE_DIRECTORY *dir, ...@@ -116,7 +116,7 @@ static int create_directory(PMEM_APPEND_CACHE_DIRECTORY *dir,
@param dir cache directory @param dir cache directory
@param cache cache descriptor @param cache cache descriptor
@param n cache slot @param cache_slot cache slot
@return @return
@retval 0 success @retval 0 success
...@@ -124,17 +124,15 @@ static int create_directory(PMEM_APPEND_CACHE_DIRECTORY *dir, ...@@ -124,17 +124,15 @@ static int create_directory(PMEM_APPEND_CACHE_DIRECTORY *dir,
*/ */
int open_cache(PMEM_APPEND_CACHE *cache, PMEM_APPEND_CACHE_DIRECTORY *dir, int open_cache(PMEM_APPEND_CACHE *cache, PMEM_APPEND_CACHE_DIRECTORY *dir,
uint64_t n) uint32_t cache_slot)
{ {
uint64_t cache_start; uint64_t cache_start;
uint64_t cache_end; uint64_t cache_end;
if (n >= dir->header->n_caches) DBUG_ASSERT(cache_slot < dir->header->n_caches);
return -1; cache_start= dir->start_offsets[cache_slot];
cache_end= cache_slot == dir->header->n_caches - 1 ?
cache_start= dir->start_offsets[n]; dir->mapped_length : dir->start_offsets[cache_slot + 1];
cache_end= n == dir->header->n_caches - 1 ? dir->mapped_length :
dir->start_offsets[n + 1];
if (cache_start < directory_header_size(dir->header->n_caches) || if (cache_start < directory_header_size(dir->header->n_caches) ||
cache_start > cache_end || cache_start > cache_end ||
...@@ -358,7 +356,7 @@ static int no_cache_sync(PMEM_APPEND_CACHE *cache, myf flags) ...@@ -358,7 +356,7 @@ static int no_cache_sync(PMEM_APPEND_CACHE *cache, myf flags)
*/ */
int pmem_append_cache_create(const char *path, uint64_t size, int pmem_append_cache_create(const char *path, uint64_t size,
uint64_t n_caches) uint32_t n_caches)
{ {
PMEM_APPEND_CACHE_DIRECTORY dir; PMEM_APPEND_CACHE_DIRECTORY dir;
int res= create_directory(&dir, path, size, n_caches); int res= create_directory(&dir, path, size, n_caches);
...@@ -437,7 +435,7 @@ int pmem_append_cache_close(PMEM_APPEND_CACHE_DIRECTORY *dir) ...@@ -437,7 +435,7 @@ int pmem_append_cache_close(PMEM_APPEND_CACHE_DIRECTORY *dir)
int pmem_append_cache_flush(PMEM_APPEND_CACHE_DIRECTORY *dir) int pmem_append_cache_flush(PMEM_APPEND_CACHE_DIRECTORY *dir)
{ {
for (uint64_t i= 0; i < dir->header->n_caches; i++) for (uint32_t i= 0; i < dir->header->n_caches; i++)
{ {
PMEM_APPEND_CACHE cache; PMEM_APPEND_CACHE cache;
MY_STAT sb; MY_STAT sb;
...@@ -493,7 +491,7 @@ int pmem_append_cache_flush(PMEM_APPEND_CACHE_DIRECTORY *dir) ...@@ -493,7 +491,7 @@ int pmem_append_cache_flush(PMEM_APPEND_CACHE_DIRECTORY *dir)
*/ */
int pmem_append_cache_init(PMEM_APPEND_CACHE_DIRECTORY *dir, const char *path, int pmem_append_cache_init(PMEM_APPEND_CACHE_DIRECTORY *dir, const char *path,
uint64_t size, uint64_t n_caches) uint64_t size, uint32_t n_caches)
{ {
if (!path) if (!path)
{ {
...@@ -522,7 +520,7 @@ int pmem_append_cache_init(PMEM_APPEND_CACHE_DIRECTORY *dir, const char *path, ...@@ -522,7 +520,7 @@ int pmem_append_cache_init(PMEM_APPEND_CACHE_DIRECTORY *dir, const char *path,
@param cache cache descriptor @param cache cache descriptor
@param dir cache directory @param dir cache directory
@param n cache slot @param cache_slot cache slot
@param file_fd file descriptor @param file_fd file descriptor
@param file_name name of file @param file_name name of file
...@@ -539,7 +537,7 @@ int pmem_append_cache_init(PMEM_APPEND_CACHE_DIRECTORY *dir, const char *path, ...@@ -539,7 +537,7 @@ int pmem_append_cache_init(PMEM_APPEND_CACHE_DIRECTORY *dir, const char *path,
int pmem_append_cache_attach(PMEM_APPEND_CACHE *cache, int pmem_append_cache_attach(PMEM_APPEND_CACHE *cache,
PMEM_APPEND_CACHE_DIRECTORY *dir, PMEM_APPEND_CACHE_DIRECTORY *dir,
uint64_t n, uint32_t cache_slot,
File file_fd, File file_fd,
const char *file_name) const char *file_name)
{ {
...@@ -566,7 +564,7 @@ int pmem_append_cache_attach(PMEM_APPEND_CACHE *cache, ...@@ -566,7 +564,7 @@ int pmem_append_cache_attach(PMEM_APPEND_CACHE *cache,
if (my_fstat(file_fd, &sb, MYF(MY_WME))) if (my_fstat(file_fd, &sb, MYF(MY_WME)))
return -1; return -1;
if ((res= open_cache(cache, dir, n))) if ((res= open_cache(cache, dir, cache_slot)))
return res; return res;
file_name_length= strlen(file_name) + 1; file_name_length= strlen(file_name) + 1;
......
...@@ -34,8 +34,8 @@ extern "C" ...@@ -34,8 +34,8 @@ extern "C"
caches. caches.
Directory header: Directory header:
magic (8 bytes) file signature magic (4 bytes) file signature
n_caches (8 bytes) number of caches in directory n_caches (4 bytes) number of caches in directory
start_offsets (8 bytes * n_caches) array of cache start offsets from start_offsets (8 bytes * n_caches) array of cache start offsets from
the beginning of the cache file the beginning of the cache file
...@@ -69,8 +69,8 @@ extern "C" ...@@ -69,8 +69,8 @@ extern "C"
/** Fixed-size directory header. */ /** Fixed-size directory header. */
typedef struct st_pmem_append_cache_directory_header typedef struct st_pmem_append_cache_directory_header
{ {
uint64_t magic; uint32_t magic;
uint64_t n_caches; uint32_t n_caches;
} PMEM_APPEND_CACHE_DIRECTORY_HEADER; } PMEM_APPEND_CACHE_DIRECTORY_HEADER;
...@@ -119,15 +119,15 @@ typedef struct st_pmem_append_cache ...@@ -119,15 +119,15 @@ typedef struct st_pmem_append_cache
int pmem_append_cache_create(const char *path, uint64_t size, int pmem_append_cache_create(const char *path, uint64_t size,
uint64_t n_caches); uint32_t n_caches);
int pmem_append_cache_open(PMEM_APPEND_CACHE_DIRECTORY *dir, const char *path); int pmem_append_cache_open(PMEM_APPEND_CACHE_DIRECTORY *dir, const char *path);
int pmem_append_cache_close(PMEM_APPEND_CACHE_DIRECTORY *dir); int pmem_append_cache_close(PMEM_APPEND_CACHE_DIRECTORY *dir);
int pmem_append_cache_flush(PMEM_APPEND_CACHE_DIRECTORY *dir); int pmem_append_cache_flush(PMEM_APPEND_CACHE_DIRECTORY *dir);
int pmem_append_cache_init(PMEM_APPEND_CACHE_DIRECTORY *dir, const char *path, int pmem_append_cache_init(PMEM_APPEND_CACHE_DIRECTORY *dir, const char *path,
uint64_t size, uint64_t n_caches); uint64_t size, uint32_t n_caches);
int pmem_append_cache_attach(PMEM_APPEND_CACHE *cache, int pmem_append_cache_attach(PMEM_APPEND_CACHE *cache,
PMEM_APPEND_CACHE_DIRECTORY *dir, PMEM_APPEND_CACHE_DIRECTORY *dir,
uint64_t n, uint32_t cache_slot,
File file_fd, File file_fd,
const char *file_name); const char *file_name);
int pmem_append_cache_detach(PMEM_APPEND_CACHE *cache); int pmem_append_cache_detach(PMEM_APPEND_CACHE *cache);
......
...@@ -55,13 +55,13 @@ static int info(int argc, char *argv[]) ...@@ -55,13 +55,13 @@ static int info(int argc, char *argv[])
perror("Failed to open cache"); perror("Failed to open cache");
return 1; return 1;
} }
printf("Number of slots in directory: %" PRIu64 ", mapped size: %zu\n", printf("Number of slots in directory: %" PRIu32 ", mapped size: %zu\n",
dir.header->n_caches, dir.header->n_caches,
dir.mapped_length); dir.mapped_length);
for (uint64_t i= 0; i < dir.header->n_caches; i++) for (uint32_t i= 0; i < dir.header->n_caches; i++)
{ {
PMEM_APPEND_CACHE cache; PMEM_APPEND_CACHE cache;
printf(" cache %" PRIu64 " at offset %" PRIu64 ": ", i, printf(" cache %" PRIu32 " at offset %" PRIu64 ": ", i,
dir.start_offsets[i]); dir.start_offsets[i]);
if (open_cache(&cache, &dir, i)) if (open_cache(&cache, &dir, i))
printf("failed to open\n"); printf("failed to open\n");
...@@ -92,7 +92,7 @@ static int create(int argc, char *argv[]) ...@@ -92,7 +92,7 @@ static int create(int argc, char *argv[])
return 1; return 1;
} }
if (pmem_append_cache_create(argv[2], strtoull(argv[3], 0, 0), if (pmem_append_cache_create(argv[2], strtoull(argv[3], 0, 0),
strtoull(argv[4], 0, 0))) strtoul(argv[4], 0, 0)))
{ {
perror("Failed to create cache"); perror("Failed to create cache");
return 1; return 1;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment