Skip to content

Commit 5692310

Browse files
committed
---
yaml --- r: 511 b: refs/heads/master c: b40a9fa h: refs/heads/master i: 509: 825f084 507: 7b602fe 503: 7344f9d 495: a4043e9 479: cd1d44e 447: 0c0acc5 383: 43b7ef3 255: ec69140 v: v3
1 parent 8065dcf commit 5692310

File tree

8 files changed

+169
-131
lines changed

8 files changed

+169
-131
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 7e62aa68018c94bcfc3fd6beab90cf7b87f91cbf
2+
refs/heads/master: b40a9fa787dd3b7d979cdf3e156284d6667fe9d2

trunk/src/Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ RUNTIME_CS := rt/sync/timer.cpp \
265265
rt/rust_message.cpp \
266266
rt/rust_timer.cpp \
267267
rt/circular_buffer.cpp \
268-
rt/isaac/randport.cpp
268+
rt/isaac/randport.cpp \
269+
rt/rust_srv.cpp
269270

270271
RUNTIME_HDR := rt/globals.h \
271272
rt/rust.h \
@@ -283,7 +284,8 @@ RUNTIME_HDR := rt/globals.h \
283284
rt/util/array_list.h \
284285
rt/util/hash_map.h \
285286
rt/sync/sync.h \
286-
rt/sync/timer.h
287+
rt/sync/timer.h \
288+
rt/rust_srv.h
287289

288290
RUNTIME_INCS := -Irt/isaac -Irt/uthash
289291
RUNTIME_OBJS := $(RUNTIME_CS:.cpp=$(CFG_OBJ_SUFFIX))

trunk/src/rt/circular_buffer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ circular_buffer::circular_buffer(rust_dom *dom, size_t unit_sz) :
3333
circular_buffer::~circular_buffer() {
3434
dom->log(rust_log::MEM, "~circular_buffer 0x%" PRIxPTR, this);
3535
I(dom, _buffer);
36-
W(dom, _unread == 0, "~circular_buffer with unread messages.");
36+
W(dom, _unread == 0, "~circular_buffer with %d unread bytes", _unread);
3737
dom->free(_buffer);
3838
}
3939

trunk/src/rt/rust.cpp

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,5 @@
11
#include "rust_internal.h"
22

3-
#define TRACK_ALLOCATIONS
4-
5-
rust_srv::rust_srv() :
6-
live_allocs(0)
7-
{
8-
}
9-
10-
rust_srv::~rust_srv()
11-
{
12-
if (live_allocs != 0) {
13-
char msg[128];
14-
snprintf(msg, sizeof(msg),
15-
"leaked memory in rust main loop (%" PRIuPTR " objects)",
16-
live_allocs);
17-
#ifdef TRACK_ALLOCATIONS
18-
for (size_t i = 0; i < allocation_list.size(); i++) {
19-
if (allocation_list[i] != NULL) {
20-
printf("allocation 0x%" PRIxPTR " was not freed\n",
21-
(uintptr_t) allocation_list[i]);
22-
}
23-
}
24-
#endif
25-
fatal(msg, __FILE__, __LINE__);
26-
}
27-
}
28-
29-
void
30-
rust_srv::log(char const *str)
31-
{
32-
printf("rt: %s\n", str);
33-
}
34-
35-
36-
37-
void *
38-
rust_srv::malloc(size_t bytes)
39-
{
40-
++live_allocs;
41-
void * val = ::malloc(bytes);
42-
#ifdef TRACK_ALLOCATIONS
43-
allocation_list.append(val);
44-
#endif
45-
return val;
46-
}
47-
48-
void *
49-
rust_srv::realloc(void *p, size_t bytes)
50-
{
51-
if (!p) {
52-
live_allocs++;
53-
}
54-
void * val = ::realloc(p, bytes);
55-
#ifdef TRACK_ALLOCATIONS
56-
if (allocation_list.replace(p, val) == false) {
57-
printf("realloc: ptr 0x%" PRIxPTR " is not in allocation_list\n",
58-
(uintptr_t) p);
59-
fatal("not in allocation_list", __FILE__, __LINE__);
60-
}
61-
#endif
62-
return val;
63-
}
64-
65-
void
66-
rust_srv::free(void *p)
67-
{
68-
#ifdef TRACK_ALLOCATIONS
69-
if (allocation_list.replace(p, NULL) == false) {
70-
printf("free: ptr 0x%" PRIxPTR " is not in allocation_list\n",
71-
(uintptr_t) p);
72-
fatal("not in allocation_list", __FILE__, __LINE__);
73-
}
74-
#endif
75-
if (live_allocs < 1) {
76-
fatal("live_allocs < 1", __FILE__, __LINE__);
77-
}
78-
live_allocs--;
79-
::free(p);
80-
}
81-
82-
void
83-
rust_srv::fatal(char const *expr, char const *file, size_t line)
84-
{
85-
char buf[1024];
86-
snprintf(buf, sizeof(buf),
87-
"fatal, '%s' failed, %s:%d",
88-
expr, file, (int)line);
89-
log(buf);
90-
exit(1);
91-
}
92-
93-
void
94-
rust_srv::warning(char const *expr, char const *file, size_t line)
95-
{
96-
char buf[1024];
97-
snprintf(buf, sizeof(buf),
98-
"warning: '%s', at: %s:%d",
99-
expr, file, (int)line);
100-
log(buf);
101-
}
102-
103-
rust_srv *
104-
rust_srv::clone()
105-
{
106-
return new rust_srv();
107-
}
1083

1094
struct
1105
command_line_args

trunk/src/rt/rust.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,7 @@
1919

2020
#include "util/array_list.h"
2121

22-
struct rust_srv {
23-
size_t live_allocs;
24-
array_list<void *> allocation_list;
25-
26-
virtual void log(char const *);
27-
virtual void fatal(char const *, char const *, size_t);
28-
virtual void warning(char const *, char const *, size_t);
29-
virtual void *malloc(size_t);
30-
virtual void *realloc(void *, size_t);
31-
virtual void free(void *);
32-
virtual rust_srv *clone();
33-
34-
rust_srv();
35-
virtual ~rust_srv();
36-
};
22+
#include "rust_srv.h"
3723

3824
inline void *operator new(size_t size, rust_srv *srv)
3925
{

trunk/src/rt/rust_internal.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <stdlib.h>
1111
#include <stdint.h>
1212
#include <inttypes.h>
13+
#include <stdarg.h>
1314

1415
#include <stdio.h>
1516
#include <string.h>
@@ -46,13 +47,14 @@ extern "C" {
4647
#error "Target CPU not supported."
4748
#endif
4849

49-
#define I(dom, e) ((e) ? (void)0 : \
50-
(dom)->srv->fatal(#e, __FILE__, __LINE__))
51-
#define W(dom, e, s) ((e) ? (void)0 : \
52-
(dom)->srv->warning(#e " : " #s, __FILE__, __LINE__))
50+
#define I(dom, e) ((e) ? (void)0 : \
51+
(dom)->srv->fatal(#e, __FILE__, __LINE__, ""))
5352

54-
#define A(dom, e, s) ((e) ? (void)0 : \
55-
(dom)->srv->fatal(#e " : " #s, __FILE__, __LINE__))
53+
#define W(dom, e, s, ...) ((e) ? (void)0 : \
54+
(dom)->srv->warning(#e, __FILE__, __LINE__, s, ## __VA_ARGS__))
55+
56+
#define A(dom, e, s, ...) ((e) ? (void)0 : \
57+
(dom)->srv->fatal(#e, __FILE__, __LINE__, s, ## __VA_ARGS__))
5658

5759
struct rust_task;
5860
struct rust_port;
@@ -164,7 +166,7 @@ template <typename T> inline T
164166
check_null(rust_dom *dom, T value, char const *expr,
165167
char const *file, size_t line) {
166168
if (value == NULL) {
167-
dom->srv->fatal(expr, file, line);
169+
dom->srv->fatal(expr, file, line, "is null");
168170
}
169171
return value;
170172
}

trunk/src/rt/rust_srv.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
*
3+
*/
4+
5+
#include "rust_internal.h"
6+
#include "rust_srv.h"
7+
8+
#define TRACK_ALLOCATIONS
9+
10+
rust_srv::rust_srv() : _live_allocations(0) {
11+
// Nop.
12+
}
13+
14+
rust_srv::~rust_srv() {
15+
if (_live_allocations != 0) {
16+
char msg[128];
17+
snprintf(msg, sizeof(msg),
18+
"leaked memory in rust main loop (%" PRIuPTR " objects)",
19+
_live_allocations);
20+
#ifdef TRACK_ALLOCATIONS
21+
for (size_t i = 0; i < _allocation_list.size(); i++) {
22+
if (_allocation_list[i] != NULL) {
23+
printf("allocation 0x%" PRIxPTR " was not freed\n",
24+
(uintptr_t) _allocation_list[i]);
25+
}
26+
}
27+
#endif
28+
fatal(msg, __FILE__, __LINE__, "");
29+
}
30+
}
31+
32+
void *
33+
rust_srv::malloc(size_t bytes) {
34+
++_live_allocations;
35+
void * val = ::malloc(bytes);
36+
#ifdef TRACK_ALLOCATIONS
37+
_allocation_list.append(val);
38+
#endif
39+
return val;
40+
}
41+
42+
void *
43+
rust_srv::realloc(void *p, size_t bytes) {
44+
if (!p) {
45+
_live_allocations++;
46+
}
47+
void * val = ::realloc(p, bytes);
48+
#ifdef TRACK_ALLOCATIONS
49+
if (_allocation_list.replace(p, val) == false) {
50+
printf("realloc: ptr 0x%" PRIxPTR " is not in allocation_list\n",
51+
(uintptr_t) p);
52+
fatal("not in allocation_list", __FILE__, __LINE__, "");
53+
}
54+
#endif
55+
return val;
56+
}
57+
58+
void
59+
rust_srv::free(void *p) {
60+
#ifdef TRACK_ALLOCATIONS
61+
if (_allocation_list.replace(p, NULL) == false) {
62+
printf("free: ptr 0x%" PRIxPTR " is not in allocation_list\n",
63+
(uintptr_t) p);
64+
fatal("not in allocation_list", __FILE__, __LINE__, "");
65+
}
66+
#endif
67+
if (_live_allocations < 1) {
68+
fatal("live_allocs < 1", __FILE__, __LINE__, "");
69+
}
70+
_live_allocations--;
71+
::free(p);
72+
}
73+
74+
void
75+
rust_srv::log(char const *msg) {
76+
printf("rt: %s\n", msg);
77+
}
78+
79+
void
80+
rust_srv::fatal(const char *expression,
81+
const char *file,
82+
size_t line,
83+
const char *format,
84+
...) {
85+
char buf[1024];
86+
va_list args;
87+
va_start(args, format);
88+
vsnprintf(buf, sizeof(buf), format, args);
89+
va_end(args);
90+
91+
char msg[1024];
92+
snprintf(msg, sizeof(msg),
93+
"fatal, '%s' failed, %s:%d %s",
94+
expression, file, (int)line, buf);
95+
log(msg);
96+
exit(1);
97+
}
98+
99+
void
100+
rust_srv::warning(char const *expression,
101+
char const *file,
102+
size_t line,
103+
const char *format,
104+
...) {
105+
char buf[1024];
106+
va_list args;
107+
va_start(args, format);
108+
vsnprintf(buf, sizeof(buf), format, args);
109+
va_end(args);
110+
111+
char msg[1024];
112+
snprintf(msg, sizeof(msg),
113+
"warning: '%s', at: %s:%d %s",
114+
expression, file, (int)line, buf);
115+
log(msg);
116+
}
117+
118+
rust_srv *
119+
rust_srv::clone() {
120+
return new rust_srv();
121+
}

trunk/src/rt/rust_srv.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
*
3+
*/
4+
5+
#ifndef RUST_SRV_H
6+
#define RUST_SRV_H
7+
8+
class rust_srv {
9+
private:
10+
size_t _live_allocations;
11+
array_list<void *> _allocation_list;
12+
public:
13+
virtual void log(char const *msg);
14+
virtual void fatal(char const *expression,
15+
char const *file,
16+
size_t line,
17+
char const *format,
18+
...);
19+
virtual void warning(char const *expression,
20+
char const *file,
21+
size_t line,
22+
char const *format,
23+
...);
24+
virtual void *malloc(size_t);
25+
virtual void *realloc(void *, size_t);
26+
virtual void free(void *);
27+
virtual rust_srv *clone();
28+
rust_srv();
29+
virtual ~rust_srv();
30+
};
31+
32+
#endif /* RUST_SRV_H */

0 commit comments

Comments
 (0)