| 2765 | } |
| 2766 | |
| 2767 | int |
| 2768 | rpmalloc_initialize_config(const rpmalloc_config_t* config) { |
| 2769 | if (_rpmalloc_initialized) { |
| 2770 | rpmalloc_thread_initialize(); |
| 2771 | return 0; |
| 2772 | } |
| 2773 | _rpmalloc_initialized = 1; |
| 2774 | |
| 2775 | if (config) |
| 2776 | memcpy(&_memory_config, config, sizeof(rpmalloc_config_t)); |
| 2777 | else |
| 2778 | _rpmalloc_memset_const(&_memory_config, 0, sizeof(rpmalloc_config_t)); |
| 2779 | |
| 2780 | if (!_memory_config.memory_map || !_memory_config.memory_unmap) { |
| 2781 | _memory_config.memory_map = _rpmalloc_mmap_os; |
| 2782 | _memory_config.memory_unmap = _rpmalloc_unmap_os; |
| 2783 | } |
| 2784 | |
| 2785 | #if PLATFORM_WINDOWS |
| 2786 | SYSTEM_INFO system_info; |
| 2787 | memset(&system_info, 0, sizeof(system_info)); |
| 2788 | GetSystemInfo(&system_info); |
| 2789 | _memory_map_granularity = system_info.dwAllocationGranularity; |
| 2790 | #else |
| 2791 | _memory_map_granularity = (size_t)sysconf(_SC_PAGESIZE); |
| 2792 | #endif |
| 2793 | |
| 2794 | #if RPMALLOC_CONFIGURABLE |
| 2795 | _memory_page_size = _memory_config.page_size; |
| 2796 | #else |
| 2797 | _memory_page_size = 0; |
| 2798 | #endif |
| 2799 | _memory_huge_pages = 0; |
| 2800 | if (!_memory_page_size) { |
| 2801 | #if PLATFORM_WINDOWS |
| 2802 | _memory_page_size = system_info.dwPageSize; |
| 2803 | #else |
| 2804 | _memory_page_size = _memory_map_granularity; |
| 2805 | if (_memory_config.enable_huge_pages) { |
| 2806 | #if defined(__linux__) |
| 2807 | size_t huge_page_size = 0; |
| 2808 | FILE* meminfo = fopen("/proc/meminfo", "r"); |
| 2809 | if (meminfo) { |
| 2810 | char line[128]; |
| 2811 | while (!huge_page_size && fgets(line, sizeof(line) - 1, meminfo)) { |
| 2812 | line[sizeof(line) - 1] = 0; |
| 2813 | if (strstr(line, "Hugepagesize:")) |
| 2814 | huge_page_size = (size_t)strtol(line + 13, 0, 10) * 1024; |
| 2815 | } |
| 2816 | fclose(meminfo); |
| 2817 | } |
| 2818 | if (huge_page_size) { |
| 2819 | _memory_huge_pages = 1; |
| 2820 | _memory_page_size = huge_page_size; |
| 2821 | _memory_map_granularity = huge_page_size; |
| 2822 | } |
| 2823 | #elif defined(__FreeBSD__) |
| 2824 | int rc; |
no test coverage detected