source: tags/ms_r18q1/UNIT_TESTER/mod_rlimit.h

Last change on this file was 16412, checked in by westram, 7 years ago
  • reintegrates 'unittest' into 'trunk'
    • limit memory for unittest code to 1Gb (not with sanitizer)
      • instantly reveals accidental allocations caused by unsigned underflow
    • fix underflows in ACI commands format + format_sequence (allocated 4Gb with certain parameters)
    • reactivate sanitizer for builds
  • adds: log:branches/unittest@16405:16411
File size: 2.3 KB
Line 
1// ================================================================= //
2//                                                                   //
3//   File      : mod_rlimit.h                                        //
4//   Purpose   : locally modify resources (e.g. available memory)    //
5//                                                                   //
6//   Coded by Ralf Westram (coder@reallysoft.de) in September 2017   //
7//   http://www.arb-home.de/                                         //
8//                                                                   //
9// ================================================================= //
10
11#ifndef MOD_RLIMIT_H
12#define MOD_RLIMIT_H
13
14#ifndef ARB_ASSERT_H
15#include <arb_assert.h>
16#endif
17#ifndef _SYS_RESOURCE_H
18#include <sys/resource.h>
19#endif
20
21class ModRLimit {
22    int    resource;
23    rlimit previous;
24    rlimit modified;
25
26    int last_errno;
27
28    bool succeeded(int res) {
29        if (res == 0) return true;
30        if (last_errno) { // dump before overwrite
31            dump_error(stderr);
32        }
33        last_errno = errno;
34        return false;
35    }
36    void dump_limitation(const rlimit& from, const rlimit& to) const {
37        fprintf(stderr, "limited resource %i to %li (was: %li of %li)\n",
38                resource,
39                to.rlim_cur,
40                from.rlim_cur,
41                from.rlim_max);
42    }
43
44public:
45
46    ModRLimit(int resource_, rlim_t newLimit) : // for resource see (manual-entry "getrlimit")
47        resource(resource_),
48        last_errno(0)
49    {
50        if (succeeded(getrlimit(resource, &previous))) {
51            modified          = previous;
52            modified.rlim_cur = newLimit;
53            if (succeeded(setrlimit(resource, &modified))) {
54                dump_limitation(previous, modified);
55            }
56        }
57        if (last_errno) dump_error(stderr);
58    }
59    ~ModRLimit() {
60        if (succeeded(setrlimit(resource, &previous))) {
61            dump_limitation(modified, previous);
62        }
63        else {
64            dump_error(stderr);
65        }
66    }
67
68    int get_errno() const { return last_errno; }
69    void dump_error(FILE *out) {
70        arb_assert(last_errno);
71        fputs("Error in ModRLimit: ", out);
72        fputs(strerror(last_errno), out);
73        fputc('\n', out);
74        last_errno = 0;
75    }
76};
77
78#else
79#error mod_rlimit.h included twice
80#endif // MOD_RLIMIT_H
Note: See TracBrowser for help on using the repository browser.