NetSurf
memory.c
Go to the documentation of this file.
1/*
2 * Copyright 2016 Chris Young <chris@unsatisfactorysoftware.co.uk>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef __amigaos4__
20#include <proto/dos.h>
21#include <proto/exec.h>
22#include <exec/interrupts.h>
23#include <stdlib.h>
24#include "amiga/memory.h"
25#include "amiga/os3support.h"
26#include "amiga/schedule.h"
27#include "content/llcache.h"
28#include "utils/log.h"
29
30ULONG __slab_max_size = 2048; /* Enable clib2's slab allocator */
31
32enum {
38};
40
41/* Special clear (ie. non-zero) */
42void *ami_memory_clear_alloc(size_t size, UBYTE value)
43{
44 void *mem = malloc(size);
45 if (mem) memset(mem, value, size);
46 return mem;
47}
48
49/* clib2 slab allocator stats */
50static int ami_memory_slab_usage_cb(const struct __slab_usage_information * sui)
51{
52 if(sui->sui_slab_index <= 1) {
53 NSLOG(netsurf, INFO, "clib2 slab usage:");
54 NSLOG(netsurf, INFO,
55 " The size of all slabs, in bytes: %ld",
56 sui->sui_slab_size);
57 NSLOG(netsurf, INFO,
58 " Number of allocations which are not managed by slabs: %ld",
59 sui->sui_num_single_allocations);
60 NSLOG(netsurf, INFO,
61 " Total number of bytes allocated for memory not managed by slabs: %ld",
62 sui->sui_total_single_allocation_size);
63 NSLOG(netsurf, INFO,
64 " Number of slabs currently in play: %ld",
65 sui->sui_num_slabs);
66 NSLOG(netsurf, INFO,
67 " Number of currently unused slabs: %ld",
68 sui->sui_num_empty_slabs);
69 NSLOG(netsurf, INFO,
70 " Number of slabs in use which are completely filled with data: %ld",
71 sui->sui_num_full_slabs);
72 NSLOG(netsurf, INFO,
73 " Total number of bytes allocated for all slabs: %ld",
74 sui->sui_total_slab_allocation_size);
75 }
76 NSLOG(netsurf, INFO, "Slab %d", sui->sui_slab_index);
77 NSLOG(netsurf, INFO, " Memory chunk size managed by this slab: %ld",
78 sui->sui_chunk_size);
79 NSLOG(netsurf, INFO,
80 " Number of memory chunks that fit in this slab: %ld",
81 sui->sui_num_chunks);
82 NSLOG(netsurf, INFO,
83 " Number of memory chunks used in this slab: %ld",
84 sui->sui_num_chunks_used);
85
86 return 0;
87}
88
89static int ami_memory_slab_alloc_cb(const struct __slab_allocation_information *sai)
90{
91 if(sai->sai_allocation_index <= 1) {
92 NSLOG(netsurf, INFO, "clib2 allocation usage:");
93 NSLOG(netsurf, INFO,
94 " Number of allocations which are not managed by slabs: %ld",
95 sai->sai_num_single_allocations);
96 NSLOG(netsurf, INFO,
97 " Total number of bytes allocated for memory not managed by slabs: %ld",
98 sai->sai_total_single_allocation_size);
99 }
100 NSLOG(netsurf, INFO, "Alloc %d", sai->sai_allocation_index);
101 NSLOG(netsurf, INFO, " Size of this allocation, as requested: %ld",
102 sai->sai_allocation_size);
103 NSLOG(netsurf, INFO,
104 " Total size of this allocation, including management data: %ld",
105 sai->sai_total_allocation_size);
106
107 return 0;
108}
109
110static int ami_memory_slab_stats_cb(void *user_data, const char *line, size_t line_length)
111{
112 BPTR fh = (BPTR)user_data;
113 long err = FPuts(fh, line);
114
115 if(err != 0) {
116 return -1;
117 } else {
118 return 0;
119 }
120}
121
123{
124 __get_slab_usage(ami_memory_slab_usage_cb);
125 __get_slab_allocations(ami_memory_slab_alloc_cb);
126 __get_slab_stats(fh, ami_memory_slab_stats_cb);
127}
128
129/* Low memory handler */
130static void ami_memory_low_mem_handler(void *p)
131{
133 NSLOG(netsurf, INFO, "Purging llcache");
134 llcache_clean(true);
136 }
137
139 NSLOG(netsurf, INFO, "Purging unused slabs");
140 __free_unused_slabs();
142 }
143}
144
145static ASM ULONG ami_memory_handler(REG(a0, struct MemHandlerData *mhd), REG(a1, void *userdata), REG(a6, struct ExecBase *execbase))
146{
149 return MEM_ALL_DONE;
150 }
151
154 }
155
158 }
159
161
162 return MEM_TRY_AGAIN;
163}
164
165struct Interrupt *ami_memory_init(void)
166{
167 struct Interrupt *memhandler = malloc(sizeof(struct Interrupt));
168 if(memhandler == NULL) return NULL; // we're screwed
169
170 memhandler->is_Node.ln_Pri = -127; // low down as will be slow
171 memhandler->is_Node.ln_Name = "NetSurf low memory handler";
172 memhandler->is_Data = NULL;
173 memhandler->is_Code = (APTR)&ami_memory_handler;
174 AddMemHandler(memhandler);
175
176 return memhandler;
177}
178
179void ami_memory_fini(struct Interrupt *memhandler)
180{
181 if(memhandler != NULL) {
182 RemMemHandler(memhandler);
183 free(memhandler);
184 }
185}
186
187#endif
188
nserror ami_schedule(int t, void(*callback)(void *p), void *p)
Schedule a callback.
Definition: schedule.c:331
void llcache_clean(bool purge)
Cause the low-level cache to attempt to perform cleanup.
Definition: llcache.c:3725
Low-level resource cache (interface)
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
ULONG __slab_max_size
Definition: memory.c:30
void * ami_memory_clear_alloc(size_t size, UBYTE value)
Definition: memory.c:42
static void ami_memory_low_mem_handler(void *p)
Definition: memory.c:130
void ami_memory_fini(struct Interrupt *memhandler)
Definition: memory.c:179
static ASM ULONG ami_memory_handler(REG(a0, struct MemHandlerData *mhd), REG(a1, void *userdata), REG(a6, struct ExecBase *execbase))
Definition: memory.c:145
static int ami_memory_slab_alloc_cb(const struct __slab_allocation_information *sai)
Definition: memory.c:89
static int low_mem_status
Definition: memory.c:39
static int ami_memory_slab_stats_cb(void *user_data, const char *line, size_t line_length)
Definition: memory.c:110
void ami_memory_slab_dump(BPTR fh)
Definition: memory.c:122
@ PURGE_NONE
Definition: memory.c:33
@ PURGE_STEP1
Definition: memory.c:34
@ PURGE_DONE_STEP1
Definition: memory.c:36
@ PURGE_STEP2
Definition: memory.c:35
@ PURGE_DONE_STEP2
Definition: memory.c:37
static int ami_memory_slab_usage_cb(const struct __slab_usage_information *sui)
Definition: memory.c:50
struct Interrupt * ami_memory_init(void)
Definition: memory.c:165
Minimal compatibility header for AmigaOS 3.
#define ASM
Definition: os3support.h:44
#define REG(reg, arg)
Definition: os3support.h:48
static nserror line(const struct redraw_context *ctx, const plot_style_t *style, const struct rect *line)
Plots a line.
Definition: plot.c:579