@@ -27,10 +27,13 @@ proc py2rlimit(limits: py_rlimit, rl_out: var RLimit) =
27
27
rl_out.rlim_cur = limits.rlim_cur
28
28
rl_out.rlim_max = limits.rlim_max
29
29
30
- proc py2rlimit(limits: py_rlimit_abc , rl_out: var RLimit) =
30
+ template py2rlimit[ T] (limits: T{atom} , rl_out: var RLimit) =
31
31
assert limits.len == 2
32
32
rl_out.rlim_cur = limits[ 0]
33
33
rl_out.rlim_max = limits[1 ]
34
+ template py2rlimit[T](limits: T, rl_out: var RLimit) =
35
+ let li = limits
36
+ py2rlimit(li, rl_out)
34
37
35
38
proc rlimit2py(rl_in: RLimit): py_rlimit = (rl_in.rlim_cur, rl_in.rlim_max)
36
39
@@ -49,15 +52,23 @@ proc getrlimit*(resource: int): py_rlimit =
49
52
50
53
proc raise_inval =
51
54
raise newException(ValueError, "current limit exceeds maximum limit")
52
- proc setrlimit* (resource: int , limits: py_rlimit_abc) =
53
- var rl: RLimit
54
- py2rlimit(limits, rl)
55
+
56
+ proc setrlimitWrap(resource: int , rl: var RLimit) =
55
57
if setrlimit(checked_resource(resource), rl) == -1:
56
58
if isErr(EINVAL): raise_inval()
57
59
elif isErr(EPERM):
58
60
raise newException(ValueError, "not allowed to raise maximum limit")
59
61
raiseErrno()
60
62
63
+ template setrlimit*[T: py_rlimit_abc|py_rlimit](resource: int , limits: T) =
64
+ ## this is defined as `template`.
65
+ ## Because if being `proc`, py_rlimit_abc match cannot work
66
+ bind py2rlimit, setrlimitWrap
67
+ mixin len, `[]`
68
+ var rl: RLimit
69
+ py2rlimit(limits, rl)
70
+ setrlimitWrap(resource, rl)
71
+
61
72
when HAVE_PRLIMIT:
62
73
proc prlimit(pid: Pid, resource: cint , new_limit: ptr RLimit, old_limit: var RLimit): cint {.
63
74
importc, header: "<sys/resource.h>".}
@@ -72,21 +83,29 @@ when HAVE_PRLIMIT:
72
83
raiseErrno()
73
84
rlimit2py(old_limit)
74
85
75
- proc prlimit* (pid: int , resource: int , limits: py_rlimit_abc): py_rlimit{.discardable.} =
76
- let
77
- pid = Pid pid
78
- resource = checked_resource(resource)
79
- var old_limit, new_limit: RLimit
80
-
81
- py2rlimit(limits, new_limit)
86
+ proc prlimitWrap(pid: Pid, resource: cint , new_limit: var RLimit): py_rlimit{.discardable.} =
87
+ var old_limit: RLimit
82
88
if prlimit(pid, resource, addr new_limit, old_limit) == -1:
83
89
if isErr(EINVAL): raise_inval()
84
90
raiseErrno()
85
-
86
91
rlimit2py(old_limit)
87
92
93
+ template prlimit*[T: py_rlimit_abc|py_rlimit](pid: int , resource: int , limits: T): py_rlimit =
94
+ ## discardable.
95
+ ##
96
+ ## this is defined as `template`.
97
+ ## Because if being `proc`, py_rlimit_abc match cannot work
98
+ bind Pid, checked_resource, RLimit, py2rlimit, prlimitWrap
99
+ mixin len, `[]`
100
+ let
101
+ tpid = Pid pid
102
+ tresource = checked_resource(resource)
103
+ var new_limit: RLimit
104
+ py2rlimit(limits, new_limit)
105
+ prlimitWrap(tpid, tresource, new_limit)
106
+
88
107
when HAVE_GETPAGESIZE:
89
- proc c_getpagesize(): cint {.importc, header: " <unistd.h>" .}
108
+ proc c_getpagesize(): cint {.importc: "getpagesize" , header: "<unistd.h>".}
90
109
proc getpagesize*(): int = int c_getpagesize()
91
110
elif HAVE_SYSCONF_PAGE_SIZE:
92
111
let SC_PAGE_SIZE{.importc, header: "<unistd.h>".}: cint
0 commit comments