src/lib/cache.c
changeset 71 01b021e406e9
parent 67 d8d9aa430ecc
child 76 2e07f7dccb67
equal deleted inserted replaced
70:35515b3a82b7 71:01b021e406e9
   206 
   206 
   207     return 0;
   207     return 0;
   208 }
   208 }
   209 
   209 
   210 /**
   210 /**
       
   211  * Compute and return the full size of the .cache file
       
   212  */
       
   213 static size_t pt_cache_size (size_t data_size)
       
   214 {
       
   215     assert(sizeof(struct pt_cache_file) == PT_CACHE_HEADER_SIZE);
       
   216 
       
   217     return sizeof(struct pt_cache_file) + data_size;
       
   218 }
       
   219 
       
   220 /**
   211  * Open the .tmp cache file as an fd for writing
   221  * Open the .tmp cache file as an fd for writing
   212  */
   222  */
   213 static int pt_cache_open_tmp_fd (struct pt_cache *cache, int *fd_ptr)
   223 static int pt_cache_open_tmp_fd (struct pt_cache *cache, int *fd_ptr)
   214 {
   224 {
   215     int fd;
   225     int fd;
   230     return 0;
   240     return 0;
   231 }
   241 }
   232 
   242 
   233 
   243 
   234 /**
   244 /**
   235  * Mmap the pt_cache_file using sizeof(struct pt_cache_file) + data_size
   245  * Mmap the pt_cache_file using pt_cache_size(data_size)
   236  */
   246  */
   237 static int pt_cache_open_mmap (struct pt_cache *cache, struct pt_cache_file **file_ptr, size_t data_size, bool readonly)
   247 static int pt_cache_open_mmap (struct pt_cache *cache, struct pt_cache_file **file_ptr, size_t data_size, bool readonly)
   238 {
   248 {
   239     int prot = 0;
   249     int prot = 0;
   240     void *addr;
   250     void *addr;
   247 
   257 
   248         prot |= PROT_WRITE;
   258         prot |= PROT_WRITE;
   249     }
   259     }
   250 
   260 
   251     // mmap() the full file including header
   261     // mmap() the full file including header
   252     if ((addr = mmap(NULL, sizeof(struct pt_cache_file) + data_size, prot, MAP_SHARED, cache->fd, 0)) == MAP_FAILED)
   262     if ((addr = mmap(NULL, pt_cache_size(data_size), prot, MAP_SHARED, cache->fd, 0)) == MAP_FAILED)
   253         RETURN_ERROR(PT_ERR_CACHE_MMAP);
   263         RETURN_ERROR(PT_ERR_CACHE_MMAP);
   254 
   264 
   255     // ok
   265     // ok
   256     *file_ptr = addr;
   266     *file_ptr = addr;
   257 
   267 
   338     // write header
   348     // write header
   339     if ((err = pt_cache_write_header(cache, header)))
   349     if ((err = pt_cache_write_header(cache, header)))
   340         JUMP_ERROR(err);
   350         JUMP_ERROR(err);
   341 
   351 
   342     // grow file
   352     // grow file
   343     if (ftruncate(cache->fd, sizeof(struct pt_cache_file) + header->data_size) < 0)
   353     if (ftruncate(cache->fd, pt_cache_size(header->data_size)) < 0)
   344         JUMP_SET_ERROR(err, PT_ERR_CACHE_TRUNC);
   354         JUMP_SET_ERROR(err, PT_ERR_CACHE_TRUNC);
   345 
   355 
   346     // mmap header and data
   356     // mmap header and data
   347     if ((err = pt_cache_open_mmap(cache, &cache->file, header->data_size, false)))
   357     if ((err = pt_cache_open_mmap(cache, &cache->file, header->data_size, false)))
   348         JUMP_ERROR(err);
   358         JUMP_ERROR(err);