forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllocationSize.c
More file actions
156 lines (140 loc) · 6.3 KB
/
AllocationSize.c
File metadata and controls
156 lines (140 loc) · 6.3 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include <stdint.h>
#include "py/objproperty.h"
#include "py/runtime.h"
#include "py/runtime0.h"
#include "shared-bindings/memorymonitor/AllocationSize.h"
#include "shared-bindings/util.h"
//| class AllocationSize:
//| def __init__(self) -> None:
//| """Tracks the number of allocations in power of two buckets.
//|
//| It will have 16 16-bit buckets to track allocation counts. It is total allocations
//| meaning frees are ignored. Reallocated memory is counted twice, at allocation and when
//| reallocated with the larger size.
//|
//| The buckets are measured in terms of blocks which is the finest granularity of the heap.
//| This means bucket 0 will count all allocations less than or equal to the number of bytes
//| per block, typically 16. Bucket 2 will be less than or equal to 4 blocks. See
//| `bytes_per_block` to convert blocks to bytes.
//|
//| Multiple AllocationSizes can be used to track different code boundaries.
//|
//| Track allocations::
//|
//| import memorymonitor
//|
//| mm = memorymonitor.AllocationSize()
//| with mm:
//| print("hello world" * 3)
//|
//| for bucket, count in enumerate(mm):
//| print("<", 2 ** bucket, count)
//|
//| """
//| ...
static mp_obj_t memorymonitor_allocationsize_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *all_args, mp_map_t *kw_args) {
memorymonitor_allocationsize_obj_t *self =
m_new_obj(memorymonitor_allocationsize_obj_t, &memorymonitor_allocationsize_type);
common_hal_memorymonitor_allocationsize_construct(self);
return MP_OBJ_FROM_PTR(self);
}
//| def __enter__(self) -> AllocationSize:
//| """Clears counts and resumes tracking."""
//| ...
static mp_obj_t memorymonitor_allocationsize_obj___enter__(mp_obj_t self_in) {
common_hal_memorymonitor_allocationsize_clear(self_in);
common_hal_memorymonitor_allocationsize_resume(self_in);
return self_in;
}
MP_DEFINE_CONST_FUN_OBJ_1(memorymonitor_allocationsize___enter___obj, memorymonitor_allocationsize_obj___enter__);
//| def __exit__(self) -> None:
//| """Automatically pauses allocation tracking when exiting a context. See
//| :ref:`lifetime-and-contextmanagers` for more info."""
//| ...
static mp_obj_t memorymonitor_allocationsize_obj___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_memorymonitor_allocationsize_pause(args[0]);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(memorymonitor_allocationsize___exit___obj, 4, 4, memorymonitor_allocationsize_obj___exit__);
//| bytes_per_block: int
//| """Number of bytes per block"""
static mp_obj_t memorymonitor_allocationsize_obj_get_bytes_per_block(mp_obj_t self_in) {
memorymonitor_allocationsize_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_memorymonitor_allocationsize_get_bytes_per_block(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(memorymonitor_allocationsize_get_bytes_per_block_obj, memorymonitor_allocationsize_obj_get_bytes_per_block);
MP_PROPERTY_GETTER(memorymonitor_allocationsize_bytes_per_block_obj,
(mp_obj_t)&memorymonitor_allocationsize_get_bytes_per_block_obj);
//| def __len__(self) -> int:
//| """Returns the number of allocation buckets.
//|
//| This allows you to::
//|
//| mm = memorymonitor.AllocationSize()
//| print(len(mm))"""
//| ...
static mp_obj_t memorymonitor_allocationsize_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
memorymonitor_allocationsize_obj_t *self = MP_OBJ_TO_PTR(self_in);
uint16_t len = common_hal_memorymonitor_allocationsize_get_len(self);
switch (op) {
case MP_UNARY_OP_BOOL:
return mp_obj_new_bool(len != 0);
case MP_UNARY_OP_LEN:
return MP_OBJ_NEW_SMALL_INT(len);
default:
return MP_OBJ_NULL; // op not supported
}
}
//| def __getitem__(self, index: int) -> Optional[int]:
//| """Returns the allocation count for the given bucket.
//|
//| This allows you to::
//|
//| mm = memorymonitor.AllocationSize()
//| print(mm[0])"""
//| ...
//|
static mp_obj_t memorymonitor_allocationsize_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t value) {
if (value == mp_const_none) {
// delete item
mp_raise_AttributeError(MP_ERROR_TEXT("Cannot delete values"));
} else {
memorymonitor_allocationsize_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (mp_obj_is_type(index_obj, &mp_type_slice)) {
mp_raise_NotImplementedError(MP_ERROR_TEXT("Slices not supported"));
} else {
size_t index = mp_get_index(&memorymonitor_allocationsize_type, common_hal_memorymonitor_allocationsize_get_len(self), index_obj, false);
if (value == MP_OBJ_SENTINEL) {
// load
return MP_OBJ_NEW_SMALL_INT(common_hal_memorymonitor_allocationsize_get_item(self, index));
} else {
mp_raise_AttributeError(MP_ERROR_TEXT("Read-only"));
}
}
}
return mp_const_none;
}
static const mp_rom_map_elem_t memorymonitor_allocationsize_locals_dict_table[] = {
// Methods
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&memorymonitor_allocationsize___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&memorymonitor_allocationsize___exit___obj) },
// Properties
{ MP_ROM_QSTR(MP_QSTR_bytes_per_block), MP_ROM_PTR(&memorymonitor_allocationsize_bytes_per_block_obj) },
};
static MP_DEFINE_CONST_DICT(memorymonitor_allocationsize_locals_dict, memorymonitor_allocationsize_locals_dict_table);
MP_DEFINE_CONST_OBJ_TYPE(
memorymonitor_allocationsize_type,
MP_QSTR_AllocationSize,
MP_TYPE_FLAG_ITER_IS_GETITER | MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS,
make_new, memorymonitor_allocationsize_make_new,
subscr, memorymonitor_allocationsize_subscr,
unary_op, memorymonitor_allocationsize_unary_op,
iter, mp_obj_generic_subscript_getiter,
locals_dict, &memorymonitor_allocationsize_locals_dict
);