diff options
| author | Eli Zaretskii | 2016-11-19 12:17:23 +0200 |
|---|---|---|
| committer | Eli Zaretskii | 2016-11-19 12:17:23 +0200 |
| commit | 4cdd14eabe5a6121691daa2d9c5e814c5f53f3e5 (patch) | |
| tree | d6c342da22c57db99b6a6b65f15ed6e29b23e22f /src/w32heap.c | |
| parent | 6cdd1c333034b308e74e70c4fd10399fbb5329b9 (diff) | |
| download | emacs-4cdd14eabe5a6121691daa2d9c5e814c5f53f3e5.tar.gz emacs-4cdd14eabe5a6121691daa2d9c5e814c5f53f3e5.zip | |
Implement getrlimit and setrlimit for MS-Windows
* src/w32heap.c (getrlimit, setrlimit): New functions.
Include w32.h.
* src/emacs.c (main): Use 'rlim_t', not 'long', for values that
should be compatible with 'struct rlimit' members.
* nt/inc/sys/resource.h: New header file.
* nt/mingw-cfg.site (ac_cv_func_getrlimit, ac_cv_func_setrlimit):
Set to "yes".
Diffstat (limited to 'src/w32heap.c')
| -rw-r--r-- | src/w32heap.c | 85 |
1 files changed, 84 insertions, 1 deletions
diff --git a/src/w32heap.c b/src/w32heap.c index 43b5f352a1d..26a04413df1 100644 --- a/src/w32heap.c +++ b/src/w32heap.c | |||
| @@ -50,9 +50,11 @@ | |||
| 50 | #include <errno.h> | 50 | #include <errno.h> |
| 51 | 51 | ||
| 52 | #include <sys/mman.h> | 52 | #include <sys/mman.h> |
| 53 | #include <sys/resource.h> | ||
| 53 | #include "w32common.h" | 54 | #include "w32common.h" |
| 54 | #include "w32heap.h" | 55 | #include "w32heap.h" |
| 55 | #include "lisp.h" | 56 | #include "lisp.h" |
| 57 | #include "w32.h" /* for FD_SETSIZE */ | ||
| 56 | 58 | ||
| 57 | /* We chose to leave those declarations here. They are used only in | 59 | /* We chose to leave those declarations here. They are used only in |
| 58 | this file. The RtlCreateHeap is available since XP. It is located | 60 | this file. The RtlCreateHeap is available since XP. It is located |
| @@ -317,6 +319,9 @@ init_heap (void) | |||
| 317 | cache_system_info (); | 319 | cache_system_info (); |
| 318 | } | 320 | } |
| 319 | 321 | ||
| 322 | |||
| 323 | /* malloc, realloc, free. */ | ||
| 324 | |||
| 320 | #undef malloc | 325 | #undef malloc |
| 321 | #undef realloc | 326 | #undef realloc |
| 322 | #undef free | 327 | #undef free |
| @@ -623,9 +628,12 @@ sbrk (ptrdiff_t increment) | |||
| 623 | return data_region_end; | 628 | return data_region_end; |
| 624 | } | 629 | } |
| 625 | 630 | ||
| 626 | #define MAX_BUFFER_SIZE (512 * 1024 * 1024) | 631 | |
| 627 | 632 | ||
| 628 | /* MMAP allocation for buffers. */ | 633 | /* MMAP allocation for buffers. */ |
| 634 | |||
| 635 | #define MAX_BUFFER_SIZE (512 * 1024 * 1024) | ||
| 636 | |||
| 629 | void * | 637 | void * |
| 630 | mmap_alloc (void **var, size_t nbytes) | 638 | mmap_alloc (void **var, size_t nbytes) |
| 631 | { | 639 | { |
| @@ -788,3 +796,78 @@ mmap_realloc (void **var, size_t nbytes) | |||
| 788 | /* Not enlarging, not shrinking by more than one page. */ | 796 | /* Not enlarging, not shrinking by more than one page. */ |
| 789 | return *var; | 797 | return *var; |
| 790 | } | 798 | } |
| 799 | |||
| 800 | |||
| 801 | /* Emulation of getrlimit and setrlimit. */ | ||
| 802 | |||
| 803 | int | ||
| 804 | getrlimit (rlimit_resource_t rltype, struct rlimit *rlp) | ||
| 805 | { | ||
| 806 | int retval = -1; | ||
| 807 | |||
| 808 | switch (rltype) | ||
| 809 | { | ||
| 810 | case RLIMIT_STACK: | ||
| 811 | { | ||
| 812 | MEMORY_BASIC_INFORMATION m; | ||
| 813 | /* Implementation note: Posix says that RLIMIT_STACK returns | ||
| 814 | information about the stack size for the main thread. The | ||
| 815 | implementation below returns the stack size for the calling | ||
| 816 | thread, so it's more like pthread_attr_getstacksize. But | ||
| 817 | Emacs clearly wants the latter, given how it uses the | ||
| 818 | results, so the implementation below is more future-proof, | ||
| 819 | if what's now the main thread will become some other thread | ||
| 820 | at some future point. */ | ||
| 821 | if (!VirtualQuery ((LPCVOID) &m, &m, sizeof m)) | ||
| 822 | errno = EPERM; | ||
| 823 | else | ||
| 824 | { | ||
| 825 | rlp->rlim_cur = (DWORD_PTR) &m - (DWORD_PTR) m.AllocationBase; | ||
| 826 | rlp->rlim_max = | ||
| 827 | (DWORD_PTR) m.BaseAddress + m.RegionSize | ||
| 828 | - (DWORD_PTR) m.AllocationBase; | ||
| 829 | |||
| 830 | /* The last page is the guard page, so subtract that. */ | ||
| 831 | rlp->rlim_cur -= getpagesize (); | ||
| 832 | rlp->rlim_max -= getpagesize (); | ||
| 833 | retval = 0; | ||
| 834 | } | ||
| 835 | } | ||
| 836 | break; | ||
| 837 | case RLIMIT_NOFILE: | ||
| 838 | /* Implementation note: The real value is returned by | ||
| 839 | _getmaxstdio. But our FD_SETSIZE is smaller, to cater to | ||
| 840 | Windows 9X, and process.c includes some logic that's based on | ||
| 841 | the assumption that the handle resource is inherited to child | ||
| 842 | processes. We want to avoid that logic, so we tell process.c | ||
| 843 | our current limit is already equal to FD_SETSIZE. */ | ||
| 844 | rlp->rlim_cur = FD_SETSIZE; | ||
| 845 | rlp->rlim_max = 2048; /* see _setmaxstdio documentation */ | ||
| 846 | retval = 0; | ||
| 847 | break; | ||
| 848 | default: | ||
| 849 | /* Note: we could return meaningful results for other RLIMIT_* | ||
| 850 | requests, but Emacs doesn't currently need that, so we just | ||
| 851 | punt for them. */ | ||
| 852 | errno = ENOSYS; | ||
| 853 | break; | ||
| 854 | } | ||
| 855 | return retval; | ||
| 856 | } | ||
| 857 | |||
| 858 | int | ||
| 859 | setrlimit (rlimit_resource_t rltype, const struct rlimit *rlp) | ||
| 860 | { | ||
| 861 | switch (rltype) | ||
| 862 | { | ||
| 863 | case RLIMIT_STACK: | ||
| 864 | case RLIMIT_NOFILE: | ||
| 865 | /* We cannot modfy these limits, so we always fail. */ | ||
| 866 | errno = EPERM; | ||
| 867 | break; | ||
| 868 | default: | ||
| 869 | errno = ENOSYS; | ||
| 870 | break; | ||
| 871 | } | ||
| 872 | return -1; | ||
| 873 | } | ||