aboutsummaryrefslogtreecommitdiff
path: root/lisp/context.c
blob: 3b94e547f34c03d2767279f418ee79a0422d921b (plain)
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
/* Copyright (C) 2025 Aryadev Chavali

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 Public License Version 2
 * for details.

 * You may distribute and modify this code under the terms of the
 * GNU General Public License Version 2, which you should have received a copy
 of along with this
 * program.  If not, please go to <https://www.gnu.org/licenses/>.

 * Created: 2025-05-12
 * Description:
 */

#include <lisp/context.h>

#include <string.h>

// Allocates against stable memory i.e. we can have pointers of this lying
// around without any fear of them being thrown away.
void *context_alloc(context_t *context, u64 size)
{
  return arena_alloc(&context->memory, size);
}

// Allocate against a "scratch space", separate from main memory, for internal
// use.
void *context_salloc(context_t *context, u64 size)
{
  return arena_alloc(&context->scratch, size);
}

void context_reset_read(context_t *context)
{
  arena_reset(&context->read);
}

void context_reset_scratch(context_t *context)
{
  arena_reset(&context->scratch);
}

void context_reset(context_t *context)
{
  arena_reset(&context->memory);
  arena_reset(&context->read);
  arena_reset(&context->scratch);
}

void context_cleanup(context_t *context)
{
  if (!context)
    return;
  arena_cleanup(&context->memory);
  arena_cleanup(&context->read);
  arena_cleanup(&context->scratch);
  memset(context, 0, sizeof(*context));
}

void context_report(context_t *context)
{
#if DEBUG
  // Figure this out at runtime
  u64 mem_used = 0, mem_cap = 0;
  for (page_t *page = context->memory.start; page; page = page->next)
  {
    mem_used += page->size;
    mem_cap += page->capacity;
  }

  u64 read_used = 0, read_cap = 0;
  for (page_t *page = context->read.start; page; page = page->next)
  {
    read_used += page->size;
    read_cap += page->capacity;
  }

  u64 scr_used = 0, scr_cap = 0;
  for (page_t *page = context->scratch.start; page; page = page->next)
  {
    scr_used += page->size;
    scr_cap += page->capacity;
  }

  info("<Context>: %luB/%luB main memory used\n", mem_used, mem_cap);
  info("<Context>: %luB/%luB read space used\n", read_used, read_cap);
  info("<Context>: %luB/%luB scratch space used\n", scr_used, scr_cap);
#endif
}