diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-09-11 19:28:07 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-09-11 19:28:07 +0000 |
commit | b4ac655acc32682a1cc430acdd2661124ecb5ba2 (patch) | |
tree | 7935e97e1e9a8f1b06c268f117d95ee851496b20 | |
parent | da47bbd1db646794fba94449d4d226d35ec2b2ff (diff) | |
download | ruby-b4ac655acc32682a1cc430acdd2661124ecb5ba2.tar.gz |
* process.c (rlimit_resource_name2int): use STRCASECMP to avoid
ALLOCA_N.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29226 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | process.c | 68 |
2 files changed, 47 insertions, 26 deletions
@@ -1,3 +1,8 @@ +Sun Sep 12 04:27:13 2010 Tanaka Akira <akr@fsij.org> + + * process.c (rlimit_resource_name2int): use STRCASECMP to avoid + ALLOCA_N. + Sat Sep 11 16:47:41 2010 Nobuyoshi Nakada <nobu@ruby-lang.org> * hash.c (ruby_setenv): raise if putenv and SetEnvironmentVariable @@ -3578,79 +3578,95 @@ proc_setpriority(VALUE obj, VALUE which, VALUE who, VALUE prio) static int rlimit_resource_name2int(const char *name, int casetype) { - size_t len = strlen(name); - if (16 < len) return -1; - if (casetype == 1) { - size_t i; - char *name2 = ALLOCA_N(char, len+1); - for (i = 0; i < len; i++) { - if (!ISLOWER(name[i])) - return -1; - name2[i] = TOUPPER(name[i]); - } - name2[len] = '\0'; - name = name2; - } - - switch (*name) { + int resource; + const char *p; +#define RESCHECK(r) \ + do { \ + if (STRCASECMP(name, #r) == 0) { \ + resource = RLIMIT_##r; \ + goto found; \ + } \ + } while (0) + + switch (TOUPPER(*name)) { case 'A': #ifdef RLIMIT_AS - if (strcmp(name, "AS") == 0) return RLIMIT_AS; + RESCHECK(AS); #endif break; case 'C': #ifdef RLIMIT_CORE - if (strcmp(name, "CORE") == 0) return RLIMIT_CORE; + RESCHECK(CORE); #endif #ifdef RLIMIT_CPU - if (strcmp(name, "CPU") == 0) return RLIMIT_CPU; + RESCHECK(CPU); #endif break; case 'D': #ifdef RLIMIT_DATA - if (strcmp(name, "DATA") == 0) return RLIMIT_DATA; + RESCHECK(DATA); #endif break; case 'F': #ifdef RLIMIT_FSIZE - if (strcmp(name, "FSIZE") == 0) return RLIMIT_FSIZE; + RESCHECK(FSIZE); #endif break; case 'M': #ifdef RLIMIT_MEMLOCK - if (strcmp(name, "MEMLOCK") == 0) return RLIMIT_MEMLOCK; + RESCHECK(MEMLOCK); #endif break; case 'N': #ifdef RLIMIT_NOFILE - if (strcmp(name, "NOFILE") == 0) return RLIMIT_NOFILE; + RESCHECK(NOFILE); #endif #ifdef RLIMIT_NPROC - if (strcmp(name, "NPROC") == 0) return RLIMIT_NPROC; + RESCHECK(NPROC); #endif break; case 'R': #ifdef RLIMIT_RSS - if (strcmp(name, "RSS") == 0) return RLIMIT_RSS; + RESCHECK(RSS); #endif break; case 'S': #ifdef RLIMIT_STACK - if (strcmp(name, "STACK") == 0) return RLIMIT_STACK; + RESCHECK(STACK); #endif #ifdef RLIMIT_SBSIZE - if (strcmp(name, "SBSIZE") == 0) return RLIMIT_SBSIZE; + RESCHECK(SBSIZE); #endif break; } return -1; + + found: + switch (casetype) { + case 0: + for (p = name; *p; p++) + if (!ISUPPER(*p)) + return -1; + break; + + case 1: + for (p = name; *p; p++) + if (!ISLOWER(*p)) + return -1; + break; + + default: + rb_bug("unexpected casetype"); + } + return resource; +#undef RESCHECK } static int |