Skip to content

Commit 0ea538b

Browse files
authored
[Fix]: make cache_init.h cpp compatible (#234)
1 parent 9406e05 commit 0ea538b

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

doc/install.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Install dependency
22

3-
libCacheSim uses [camke](https://cmake.org/) build system and has a few dependencies:
3+
libCacheSim uses [cmake](https://cmake.org/) build system and has a few dependencies:
44
[glib](https://developer.gnome.org/glib/)
55
[tcmalloc](https://github.com/google/tcmalloc),
66
[zstd](https://github.com/facebook/zstd).
@@ -70,4 +70,4 @@ brew install zstd
7070
If still shows zstd not found after compilation, try
7171
```bash
7272
brew link zstd
73-
```
73+
```

libCacheSim/bin/cachesim/cache_init.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
#include <strings.h>
3+
#ifndef _GNU_SOURCE
34
#define _GNU_SOURCE
5+
#endif
46
#include <stdio.h>
57
#include <stdlib.h>
68
#include <string.h>
@@ -105,9 +107,19 @@ static inline cache_t *create_cache(const char *trace_path,
105107
} else {
106108
const char *window_size = strstr(eviction_params, "window-size=");
107109
if (window_size == NULL) {
108-
char *new_params = malloc(strlen(eviction_params) + 20);
109-
sprintf(new_params, "%s,window-size=0.01", eviction_params);
110+
// Calculate exact size needed: original + ",window-size=0.01" + null
111+
// terminator
112+
size_t new_params_len =
113+
strlen(eviction_params) + strlen(",window-size=0.01") + 1;
114+
char *new_params = (char *)malloc(new_params_len);
115+
if (new_params == NULL) {
116+
ERROR("failed to allocate memory for new_params\n");
117+
abort();
118+
}
119+
snprintf(new_params, new_params_len, "%s,window-size=0.01",
120+
eviction_params);
110121
cache = WTinyLFU_init(cc_params, new_params);
122+
free(new_params); // Free the allocated memory
111123
} else {
112124
cache = WTinyLFU_init(cc_params, eviction_params);
113125
}

0 commit comments

Comments
 (0)