forked from brltty/brltty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addresses.c
168 lines (134 loc) · 3.45 KB
/
addresses.c
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
157
158
159
160
161
162
163
164
165
166
167
168
/*
* BRLTTY - A background process providing access to the console screen (when in
* text mode) for a blind person using a refreshable braille display.
*
* Copyright (C) 1995-2022 by The BRLTTY Developers.
*
* BRLTTY comes with ABSOLUTELY NO WARRANTY.
*
* This is free software, placed under the terms of the
* GNU Lesser General Public License, as published by the Free Software
* Foundation; either version 2.1 of the License, or (at your option) any
* later version. Please see the file LICENSE-LGPL for details.
*
* Web Page: http://brltty.app/
*
* This software is maintained by Dave Mielke <[email protected]>.
*/
#include "prologue.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "addresses.h"
#include "log.h"
#include "dynld.h"
#include "program.h"
typedef struct {
void *address;
char name[0];
} AddressEntry;
static AddressEntry **addressTable = NULL;
static int addressCount = 0;
static int addressLimit = 0;
static void
exitAddressTable (void *data) {
if (addressTable) free(addressTable);
addressTable = NULL;
addressCount = 0;
addressLimit = 0;
}
static int
findAddressIndex (int *index, void *address) {
int first = 0;
int last = addressCount - 1;
while (first <= last) {
int current = (first + last) / 2;
AddressEntry *entry = addressTable[current];
if (entry->address == address) {
*index = current;
return 1;
}
if (address < entry->address) {
last = current - 1;
} else {
first = current + 1;
}
}
*index = first;
return 0;
}
static void
moveAddressSlice (int to, int from, int count) {
memmove(&addressTable[to], &addressTable[from], ARRAY_SIZE(addressTable, count));
}
static int
insertAddressEntry (int index, AddressEntry *entry) {
if (addressCount == addressLimit) {
int newLimit = addressLimit + 1;
AddressEntry **newTable = realloc(addressTable, ARRAY_SIZE(newTable, newLimit));
if (!newTable) {
logMallocError();
return 0;
}
if (!addressTable) {
onProgramExit("address-table", exitAddressTable, NULL);
}
addressTable = newTable;
addressLimit = newLimit;
}
moveAddressSlice(index+1, index, (addressCount++ - index));
addressTable[index] = entry;
return 1;
}
static void
removeAddressEntry (int index) {
free(addressTable[index]);
moveAddressSlice(index, index+1, (--addressCount - index));
}
int
setAddressName (void *address, const char *format, ...) {
char name[0X1000];
AddressEntry *entry;
size_t size;
{
va_list arguments;
va_start(arguments, format);
vsnprintf(name, sizeof(name), format, arguments);
va_end(arguments);
}
size = sizeof(*entry) + strlen(name) + 1;
if ((entry = malloc(size))) {
int index;
memset(entry, 0, sizeof(*entry));
entry->address = address;
strcpy(entry->name, name);
if (findAddressIndex(&index, address)) {
free(addressTable[index]);
addressTable[index] = entry;
return 1;
}
if (insertAddressEntry(index, entry)) return 1;
free(entry);
} else {
logMallocError();
}
return 0;
}
void
unsetAddressName (void *address) {
int index;
if (findAddressIndex(&index, address)) {
removeAddressEntry(index);
}
}
const char *
getAddressName (void *address, ptrdiff_t *offset) {
{
int index;
if (findAddressIndex(&index, address)) {
if (offset) *offset = 0;
return addressTable[index]->name;
}
}
return getSharedSymbolName(address, offset);
}