pt_cache_size
authorTero Marttila <terom@fixme.fi>
Mon, 25 Jan 2010 02:48:41 +0200
changeset 71 01b021e406e9
parent 70 35515b3a82b7
child 72 c3f8502cfd97
pt_cache_size
src/lib/cache.c
src/lib/cache.h
--- a/src/lib/cache.c	Mon Jan 25 02:43:05 2010 +0200
+++ b/src/lib/cache.c	Mon Jan 25 02:48:41 2010 +0200
@@ -208,6 +208,16 @@
 }
 
 /**
+ * Compute and return the full size of the .cache file
+ */
+static size_t pt_cache_size (size_t data_size)
+{
+    assert(sizeof(struct pt_cache_file) == PT_CACHE_HEADER_SIZE);
+
+    return sizeof(struct pt_cache_file) + data_size;
+}
+
+/**
  * Open the .tmp cache file as an fd for writing
  */
 static int pt_cache_open_tmp_fd (struct pt_cache *cache, int *fd_ptr)
@@ -232,7 +242,7 @@
 
 
 /**
- * Mmap the pt_cache_file using sizeof(struct pt_cache_file) + data_size
+ * Mmap the pt_cache_file using pt_cache_size(data_size)
  */
 static int pt_cache_open_mmap (struct pt_cache *cache, struct pt_cache_file **file_ptr, size_t data_size, bool readonly)
 {
@@ -249,7 +259,7 @@
     }
 
     // mmap() the full file including header
-    if ((addr = mmap(NULL, sizeof(struct pt_cache_file) + data_size, prot, MAP_SHARED, cache->fd, 0)) == MAP_FAILED)
+    if ((addr = mmap(NULL, pt_cache_size(data_size), prot, MAP_SHARED, cache->fd, 0)) == MAP_FAILED)
         RETURN_ERROR(PT_ERR_CACHE_MMAP);
 
     // ok
@@ -340,7 +350,7 @@
         JUMP_ERROR(err);
 
     // grow file
-    if (ftruncate(cache->fd, sizeof(struct pt_cache_file) + header->data_size) < 0)
+    if (ftruncate(cache->fd, pt_cache_size(header->data_size)) < 0)
         JUMP_SET_ERROR(err, PT_ERR_CACHE_TRUNC);
 
     // mmap header and data
--- a/src/lib/cache.h	Mon Jan 25 02:43:05 2010 +0200
+++ b/src/lib/cache.h	Mon Jan 25 02:48:41 2010 +0200
@@ -47,7 +47,7 @@
 };
 
 /**
- * On-disk data format. This struct is always exactly PT_CACHE_HEADER_SIZE long
+ * On-disk data format. This struct is always exactly PT_CACHE_HEADER_SIZE long (or is it?)
  */
 struct pt_cache_file {
     /** Header */