Create the sys/resource.h portability header
authorChristopher Dykes <cdykes@fb.com>
Thu, 31 Mar 2016 17:49:17 +0000 (10:49 -0700)
committerFacebook Github Bot 7 <facebook-github-bot-7-bot@fb.com>
Thu, 31 Mar 2016 17:50:27 +0000 (10:50 -0700)
Summary: Windows doesn't have it, so be nice and at least stub it out.

Reviewed By: yfeldblum

Differential Revision: D2984232

fb-gh-sync-id: 3e8871ab78c5d7c8785a52af24548245f842f19b
fbshipit-source-id: 3e8871ab78c5d7c8785a52af24548245f842f19b

folly/Makefile.am
folly/portability/SysResource.cpp [new file with mode: 0755]
folly/portability/SysResource.h [new file with mode: 0755]

index 0e0357c0acd3aedf9d9fbe2a5d5da518b0ad1f25..44d109c33ab57196aae753b2a8acc54ab0899ca5 100644 (file)
@@ -279,6 +279,7 @@ nobase_follyinclude_HEADERS = \
        portability/Strings.h \
        portability/SysFile.h \
        portability/SysMman.h \
        portability/Strings.h \
        portability/SysFile.h \
        portability/SysMman.h \
+       portability/SysResource.h \
        portability/SysStat.h \
        portability/SysSyscall.h \
        portability/SysTime.h \
        portability/SysStat.h \
        portability/SysSyscall.h \
        portability/SysTime.h \
@@ -420,6 +421,7 @@ libfolly_la_SOURCES = \
        portability/Strings.cpp \
        portability/SysFile.cpp \
        portability/SysMman.cpp \
        portability/Strings.cpp \
        portability/SysFile.cpp \
        portability/SysMman.cpp \
+       portability/SysResource.cpp \
        portability/SysStat.cpp \
        portability/SysTime.cpp \
        portability/Time.cpp \
        portability/SysStat.cpp \
        portability/SysTime.cpp \
        portability/Time.cpp \
diff --git a/folly/portability/SysResource.cpp b/folly/portability/SysResource.cpp
new file mode 100755 (executable)
index 0000000..f5b94cc
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2016 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <folly/portability/SysResource.h>
+
+#ifdef _WIN32
+#include <folly/portability/Windows.h>
+
+extern "C" {
+int getrlimit(int type, rlimit* dst) {
+  if (type == RLIMIT_STACK) {
+    NT_TIB* tib = (NT_TIB*)NtCurrentTeb();
+    dst->rlim_cur = (size_t)tib->StackBase - (size_t)tib->StackLimit;
+    dst->rlim_max = dst->rlim_cur;
+    return 0;
+  }
+  return -1;
+}
+
+int getrusage(int who, rusage* usage) {
+  // You get NOTHING! Good day to you sir.
+  ZeroMemory(usage, sizeof(rusage));
+  return 0;
+}
+
+int setrlimit(int type, rlimit* src) {
+  // Do nothing for setting them for now.
+  // We couldn't set the stack size at runtime even if we wanted to.
+  return 0;
+}
+}
+#endif
diff --git a/folly/portability/SysResource.h b/folly/portability/SysResource.h
new file mode 100755 (executable)
index 0000000..c82d933
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2016 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#ifndef _WIN32
+#include <sys/resource.h>
+#else
+#include <cstdint>
+
+#include <folly/portability/SysTime.h>
+
+#define RLIMIT_CORE 0
+#define RLIMIT_NOFILE 0
+#define RLIMIT_DATA 0
+#define RLIMIT_STACK 3
+#define RLIM_INFINITY SIZE_MAX
+
+#define RUSAGE_SELF 0
+#define RUSAGE_CHILDREN 0
+#define RUSAGE_THREAD 0
+
+using rlim_t = size_t;
+struct rlimit {
+  rlim_t rlim_cur;
+  rlim_t rlim_max;
+};
+
+struct rusage {
+  timeval ru_utime;
+  timeval ru_stime;
+  long ru_maxrss;
+  long ru_ixrss;
+  long ru_idrss;
+  long ru_isrss;
+  long ru_minflt;
+  long ru_majflt;
+  long ru_nswap;
+  long ru_inblock;
+  long ru_oublock;
+  long ru_msgsnd;
+  long ru_msgrcv;
+  long ru_nsignals;
+  long ru_nvcsw;
+  long ru_nivcsw;
+};
+
+extern "C" {
+int getrlimit(int type, rlimit* dst);
+int getrusage(int who, rusage* usage);
+int setrlimit(int type, rlimit* src);
+}
+#endif