aboutsummaryrefslogtreecommitdiff
path: root/test/symbolic/klee/nvim/memory.c
blob: 1614f813d7c3a69d53fad4ab3e5b1386d8f32ecf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdlib.h>
#include <assert.h>

#include "nvim/lib/ringbuf.h"

enum { RB_SIZE = 1024 };

typedef struct {
  void *ptr;
  size_t size;
} AllocRecord;

RINGBUF_TYPEDEF(AllocRecords, AllocRecord)
RINGBUF_INIT(AllocRecords, arecs, AllocRecord, RINGBUF_DUMMY_FREE)
RINGBUF_STATIC(static, AllocRecords, AllocRecord, arecs, RB_SIZE)

size_t allocated_memory = 0;
size_t ever_allocated_memory = 0;

size_t allocated_memory_limit = SIZE_MAX;

void *xmalloc(const size_t size)
{
  void *ret = malloc(size);
  allocated_memory += size;
  ever_allocated_memory += size;
  assert(allocated_memory <= allocated_memory_limit);
  assert(arecs_rb_length(&arecs) < RB_SIZE);
  arecs_rb_push(&arecs, (AllocRecord) {
    .ptr = ret,
    .size = size,
  });
  return ret;
}

void xfree(void *const p)
{
  if (p == NULL) {
    return;
  }
  RINGBUF_FORALL(&arecs, AllocRecord, arec) {
    if (arec->ptr == p) {
      allocated_memory -= arec->size;
      arecs_rb_remove(&arecs, arecs_rb_find_idx(&arecs, arec));
      return;
    }
  }
  abort();
}

void *xrealloc(void *const p, size_t new_size)
{
  void *ret = realloc(p, new_size);
  RINGBUF_FORALL(&arecs, AllocRecord, arec) {
    if (arec->ptr == p) {
      allocated_memory -= arec->size;
      allocated_memory += new_size;
      if (new_size > arec->size) {
        ever_allocated_memory += (new_size - arec->size);
      }
      arec->ptr = ret;
      arec->size = new_size;
      return ret;
    }
  }
  abort();
  return (void *)(intptr_t)1;
}

char *xstrdup(const char *str)
  FUNC_ATTR_MALLOC FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_RET
  FUNC_ATTR_NONNULL_ALL
{
  return xmemdupz(str, strlen(str));
}

void *xmallocz(size_t size)
  FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_RET FUNC_ATTR_WARN_UNUSED_RESULT
{
  size_t total_size = size + 1;
  assert(total_size > size);

  void *ret = xmalloc(total_size);
  ((char *)ret)[size] = 0;

  return ret;
}

char *xstpcpy(char *restrict dst, const char *restrict src)
  FUNC_ATTR_NONNULL_RET FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
{
  const size_t len = strlen(src);
  return (char *)memcpy(dst, src, len + 1) + len;
}

void *xmemdupz(const void *data, size_t len)
  FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_RET FUNC_ATTR_WARN_UNUSED_RESULT
  FUNC_ATTR_NONNULL_ALL
{
  return memcpy(xmallocz(len), data, len);
}