New upstream version 21.0.2+dfsg1

This commit is contained in:
Sebastian Ramacher 2018-02-19 20:54:37 +01:00
parent 1f1bbb3518
commit baafb6325b
706 changed files with 49633 additions and 5044 deletions

View file

@ -76,7 +76,7 @@ EXPORT void base_set_crash_handler(
EXPORT void blogva(int log_level, const char *format, va_list args);
#ifndef _MSC_VER
#if !defined(_MSC_VER) && !defined(SWIG)
#define PRINTFATTR(f, a) __attribute__((__format__(__printf__, f, a)))
#else
#define PRINTFATTR(f, a)

View file

@ -189,6 +189,48 @@ static inline void circlebuf_push_front(struct circlebuf *cb, const void *data,
}
}
static inline void circlebuf_push_back_zero(struct circlebuf *cb, size_t size)
{
size_t new_end_pos = cb->end_pos + size;
cb->size += size;
circlebuf_ensure_capacity(cb);
if (new_end_pos > cb->capacity) {
size_t back_size = cb->capacity - cb->end_pos;
size_t loop_size = size - back_size;
if (back_size)
memset((uint8_t*)cb->data + cb->end_pos, 0, back_size);
memset(cb->data, 0, loop_size);
new_end_pos -= cb->capacity;
} else {
memset((uint8_t*)cb->data + cb->end_pos, 0, size);
}
cb->end_pos = new_end_pos;
}
static inline void circlebuf_push_front_zero(struct circlebuf *cb, size_t size)
{
cb->size += size;
circlebuf_ensure_capacity(cb);
if (cb->start_pos < size) {
size_t back_size = size - cb->start_pos;
if (cb->start_pos)
memset(cb->data, 0, cb->start_pos);
cb->start_pos = cb->capacity - back_size;
memset((uint8_t*)cb->data + cb->start_pos, 0, back_size);
} else {
cb->start_pos -= size;
memset((uint8_t*)cb->data + cb->start_pos, 0, size);
}
}
static inline void circlebuf_peek_front(struct circlebuf *cb, void *data,
size_t size)
{
@ -237,6 +279,11 @@ static inline void circlebuf_pop_front(struct circlebuf *cb, void *data,
circlebuf_peek_front(cb, data, size);
cb->size -= size;
if (!cb->size) {
cb->start_pos = cb->end_pos = 0;
return;
}
cb->start_pos += size;
if (cb->start_pos >= cb->capacity)
cb->start_pos -= cb->capacity;
@ -248,6 +295,11 @@ static inline void circlebuf_pop_back(struct circlebuf *cb, void *data,
circlebuf_peek_front(cb, data, size);
cb->size -= size;
if (!cb->size) {
cb->start_pos = cb->end_pos = 0;
return;
}
if (cb->end_pos <= size)
cb->end_pos = cb->capacity - (size - cb->end_pos);
else

View file

@ -262,49 +262,78 @@ wchar_t *wcsdepad(wchar_t *str)
char **strlist_split(const char *str, char split_ch, bool include_empty)
{
const char *cur_str = str;
const char *next_str;
const char *new_str;
DARRAY(char*) list;
da_init(list);
const char *cur_str = str;
const char *next_str;
char * out = NULL;
size_t count = 0;
size_t total_size = 0;
if (str) {
char **table;
char *offset;
size_t cur_idx = 0;
size_t cur_pos = 0;
next_str = strchr(str, split_ch);
while (next_str) {
size_t size = next_str - cur_str;
if (size || include_empty) {
new_str = bstrdup_n(cur_str, size);
da_push_back(list, &new_str);
++count;
total_size += size + 1;
}
cur_str = next_str+1;
cur_str = next_str + 1;
next_str = strchr(cur_str, split_ch);
}
if (*cur_str || include_empty) {
new_str = bstrdup(cur_str);
da_push_back(list, &new_str);
++count;
total_size += strlen(cur_str) + 1;
}
/* ------------------ */
cur_pos = (count + 1) * sizeof(char *);
total_size += cur_pos;
out = bmalloc(total_size);
offset = out + cur_pos;
table = (char **)out;
/* ------------------ */
next_str = strchr(str, split_ch);
cur_str = str;
while (next_str) {
size_t size = next_str - cur_str;
if (size || include_empty) {
table[cur_idx++] = offset;
strncpy(offset, cur_str, size);
offset[size] = 0;
offset += size + 1;
}
cur_str = next_str + 1;
next_str = strchr(cur_str, split_ch);
}
if (*cur_str || include_empty) {
table[cur_idx++] = offset;
strcpy(offset, cur_str);
}
table[cur_idx] = NULL;
}
new_str = NULL;
da_push_back(list, &new_str);
return list.array;
return (char**)out;
}
void strlist_free(char **strlist)
{
if (strlist) {
char **temp = strlist;
while (*temp)
bfree(*(temp++));
bfree(strlist);
}
bfree(strlist);
}
void dstr_init_copy_strref(struct dstr *dst, const struct strref *src)

View file

@ -352,3 +352,68 @@ int os_get_logical_cores(void)
os_get_cores_internal();
return logical_cores;
}
static inline bool os_get_sys_memory_usage_internal(vm_statistics_t vmstat)
{
mach_msg_type_number_t out_count = HOST_VM_INFO_COUNT;
if (host_statistics(mach_host_self(), HOST_VM_INFO,
(host_info_t)vmstat, &out_count) != KERN_SUCCESS)
return false;
return true;
}
uint64_t os_get_sys_free_size(void)
{
vm_statistics_data_t vmstat = {};
if (!os_get_sys_memory_usage_internal(&vmstat))
return 0;
return vmstat.free_count * vm_page_size;
}
#ifndef MACH_TASK_BASIC_INFO
typedef task_basic_info_data_t mach_task_basic_info_data_t;
#endif
static inline bool os_get_proc_memory_usage_internal(
mach_task_basic_info_data_t *taskinfo)
{
#ifdef MACH_TASK_BASIC_INFO
const task_flavor_t flavor = MACH_TASK_BASIC_INFO;
mach_msg_type_number_t out_count = MACH_TASK_BASIC_INFO_COUNT;
#else
const task_flavor_t flavor = TASK_BASIC_INFO;
mach_msg_type_number_t out_count = TASK_BASIC_INFO_COUNT;
#endif
if (task_info(mach_task_self(), flavor,
(task_info_t)taskinfo, &out_count) != KERN_SUCCESS)
return false;
return true;
}
bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage)
{
mach_task_basic_info_data_t taskinfo = {};
if (!os_get_proc_memory_usage_internal(&taskinfo))
return false;
usage->resident_size = taskinfo.resident_size;
usage->virtual_size = taskinfo.virtual_size;
return true;
}
uint64_t os_get_proc_resident_size(void)
{
mach_task_basic_info_data_t taskinfo = {};
if (!os_get_proc_memory_usage_internal(&taskinfo))
return 0;
return taskinfo.resident_size;
}
uint64_t os_get_proc_virtual_size(void)
{
mach_task_basic_info_data_t taskinfo = {};
if (!os_get_proc_memory_usage_internal(&taskinfo))
return 0;
return taskinfo.virtual_size;
}

View file

@ -33,6 +33,16 @@
#if !defined(__APPLE__)
#include <sys/times.h>
#include <sys/wait.h>
#ifdef __FreeBSD__
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/user.h>
#include <unistd.h>
#include <libprocstat.h>
#else
#include <sys/resource.h>
#endif
#include <spawn.h>
#endif
@ -628,8 +638,6 @@ static int physical_cores = 0;
static int logical_cores = 0;
static bool core_count_initialized = false;
/* return sysconf(_SC_NPROCESSORS_ONLN); */
static void os_get_cores_internal(void)
{
if (core_count_initialized)
@ -639,29 +647,120 @@ static void os_get_cores_internal(void)
logical_cores = sysconf(_SC_NPROCESSORS_ONLN);
#ifndef __linux__
physical_cores = logical_cores;
#else
char *text = os_quick_read_utf8_file("/proc/cpuinfo");
char *core_id = text;
#if defined(__linux__)
int physical_id = -1;
int last_physical_id = -1;
int core_count = 0;
char *line = NULL;
size_t linecap = 0;
FILE *fp;
struct dstr proc_phys_id;
struct dstr proc_phys_ids;
fp = fopen("/proc/cpuinfo", "r");
if (!fp)
return;
dstr_init(&proc_phys_id);
dstr_init(&proc_phys_ids);
while (getline(&line, &linecap, fp) != -1) {
if (!strncmp(line, "physical id", 11)) {
char *start = strchr(line, ':');
if (!start || *(++start) == '\0')
continue;
physical_id = atoi(start);
dstr_free(&proc_phys_id);
dstr_init(&proc_phys_id);
dstr_catf(&proc_phys_id, "%d", physical_id);
}
if (!strncmp(line, "cpu cores", 9)) {
char *start = strchr(line, ':');
if (!start || *(++start) == '\0')
continue;
if (dstr_is_empty(&proc_phys_ids) ||
(!dstr_is_empty(&proc_phys_ids) &&
!dstr_find(&proc_phys_ids, proc_phys_id.array))) {
dstr_cat_dstr(&proc_phys_ids, &proc_phys_id);
dstr_cat(&proc_phys_ids, " ");
core_count += atoi(start);
}
}
if (*line == '\n' && physical_id != last_physical_id) {
last_physical_id = physical_id;
}
}
if (core_count == 0)
physical_cores = logical_cores;
else
physical_cores = core_count;
fclose(fp);
dstr_free(&proc_phys_ids);
dstr_free(&proc_phys_id);
free(line);
#elif defined(__FreeBSD__)
char *text = os_quick_read_utf8_file("/var/run/dmesg.boot");
char *core_count = text;
int packages = 0;
int cores = 0;
struct dstr proc_packages;
struct dstr proc_cores;
dstr_init(&proc_packages);
dstr_init(&proc_cores);
if (!text || !*text) {
physical_cores = logical_cores;
return;
}
for (;;) {
core_id = strstr(core_id, "\ncore id");
if (!core_id)
break;
physical_cores++;
core_id++;
}
core_count = strstr(core_count, "\nFreeBSD/SMP: ");
if (!core_count)
goto FreeBSD_cores_cleanup;
if (physical_cores == 0)
core_count++;
core_count = strstr(core_count, "\nFreeBSD/SMP: ");
if (!core_count)
goto FreeBSD_cores_cleanup;
core_count = strstr(core_count, ": ");
core_count += 2;
size_t len = strcspn(core_count, " ");
dstr_ncopy(&proc_packages, core_count, len);
core_count = strstr(core_count, "package(s) x ");
if (!core_count)
goto FreeBSD_cores_cleanup;
core_count += 13;
len = strcspn(core_count, " ");
dstr_ncopy(&proc_cores, core_count, len);
FreeBSD_cores_cleanup:
if (!dstr_is_empty(&proc_packages))
packages = atoi(proc_packages.array);
if (!dstr_is_empty(&proc_cores))
cores = atoi(proc_cores.array);
if (packages == 0)
physical_cores = logical_cores;
else if (cores == 0)
physical_cores = packages;
else
physical_cores = packages * cores;
dstr_free(&proc_cores);
dstr_free(&proc_packages);
bfree(text);
#else
physical_cores = logical_cores;
#endif
}
@ -678,6 +777,120 @@ int os_get_logical_cores(void)
os_get_cores_internal();
return logical_cores;
}
#ifdef __FreeBSD__
uint64_t os_get_sys_free_size(void)
{
uint64_t mem_free = 0;
size_t length = sizeof(mem_free);
if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free, &length,
NULL, 0) < 0)
return 0;
return mem_free;
}
static inline bool os_get_proc_memory_usage_internal(struct kinfo_proc *kinfo)
{
int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()};
size_t length = sizeof(*kinfo);
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), kinfo, &length,
NULL, 0) < 0)
return false;
return true;
}
bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage)
{
struct kinfo_proc kinfo;
if (!os_get_proc_memory_usage_internal(&kinfo))
return false;
usage->resident_size =
(uint64_t)kinfo.ki_rssize * sysconf(_SC_PAGESIZE);
usage->virtual_size = (uint64_t)kinfo.ki_size;
return true;
}
uint64_t os_get_proc_resident_size(void)
{
struct kinfo_proc kinfo;
if (!os_get_proc_memory_usage_internal(&kinfo))
return 0;
return (uint64_t)kinfo.ki_rssize * sysconf(_SC_PAGESIZE);
}
uint64_t os_get_proc_virtual_size(void)
{
struct kinfo_proc kinfo;
if (!os_get_proc_memory_usage_internal(&kinfo))
return 0;
return (uint64_t)kinfo.ki_size;
}
#else
uint64_t os_get_sys_free_size(void) {return 0;}
typedef struct
{
unsigned long virtual_size;
unsigned long resident_size;
unsigned long share_pages;
unsigned long text;
unsigned long library;
unsigned long data;
unsigned long dirty_pages;
} statm_t;
static inline bool os_get_proc_memory_usage_internal(statm_t *statm)
{
const char *statm_path = "/proc/self/statm";
FILE *f = fopen(statm_path, "r");
if (!f)
return false;
if (fscanf(f, "%ld %ld %ld %ld %ld %ld %ld",
&statm->virtual_size,
&statm->resident_size,
&statm->share_pages,
&statm->text,
&statm->library,
&statm->data,
&statm->dirty_pages) != 7) {
fclose(f);
return false;
}
fclose(f);
return true;
}
bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage)
{
statm_t statm = {};
if (!os_get_proc_memory_usage_internal(&statm))
return false;
usage->resident_size = statm.resident_size;
usage->virtual_size = statm.virtual_size;
return true;
}
uint64_t os_get_proc_resident_size(void)
{
statm_t statm = {};
if (!os_get_proc_memory_usage_internal(&statm))
return 0;
return (uint64_t)statm.resident_size;
}
uint64_t os_get_proc_virtual_size(void)
{
statm_t statm = {};
if (!os_get_proc_memory_usage_internal(&statm))
return 0;
return (uint64_t)statm.virtual_size;
}
#endif
#endif
uint64_t os_get_free_disk_space(const char *dir)

View file

@ -14,16 +14,19 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#define PSAPI_VERSION 1
#include <windows.h>
#include <mmsystem.h>
#include <shellapi.h>
#include <shlobj.h>
#include <intrin.h>
#include <psapi.h>
#include "base.h"
#include "platform.h"
#include "darray.h"
#include "dstr.h"
#include "windows/win-registry.h"
#include "windows/win-version.h"
#include "../../deps/w32-pthreads/pthread.h"
@ -47,7 +50,7 @@ static inline uint32_t get_winver(void)
winver = (ver.major << 16) | ver.minor;
}
return winver;
return winver;
}
void *os_dlopen(const char *path)
@ -798,6 +801,32 @@ bool is_64_bit_windows(void)
#endif
}
void get_reg_dword(HKEY hkey, LPCWSTR sub_key, LPCWSTR value_name,
struct reg_dword *info)
{
struct reg_dword reg = {0};
HKEY key;
LSTATUS status;
status = RegOpenKeyEx(hkey, sub_key, 0, KEY_READ, &key);
if (status != ERROR_SUCCESS) {
info->status = status;
info->size = 0;
info->return_value = 0;
return;
}
reg.size = sizeof(reg.return_value);
reg.status = RegQueryValueExW(key, value_name, NULL, NULL,
(LPBYTE)&reg.return_value, &reg.size);
RegCloseKey(key);
*info = reg;
}
#define WINVER_REG_KEY L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"
void get_win_ver(struct win_version_info *info)
@ -812,7 +841,7 @@ void get_win_ver(struct win_version_info *info)
get_dll_ver(L"kernel32", &ver);
got_version = true;
if (ver.major == 10 && ver.revis == 0) {
if (ver.major == 10) {
HKEY key;
DWORD size, win10_revision;
LSTATUS status;
@ -827,7 +856,8 @@ void get_win_ver(struct win_version_info *info)
status = RegQueryValueExW(key, L"UBR", NULL, NULL,
(LPBYTE)&win10_revision, &size);
if (status == ERROR_SUCCESS)
ver.revis = (int)win10_revision;
ver.revis = (int)win10_revision > ver.revis ?
(int)win10_revision : ver.revis;
RegCloseKey(key);
}
@ -947,6 +977,55 @@ int os_get_logical_cores(void)
return logical_cores;
}
static inline bool os_get_sys_memory_usage_internal(MEMORYSTATUSEX *msex)
{
if (!GlobalMemoryStatusEx(msex))
return false;
return true;
}
uint64_t os_get_sys_free_size(void)
{
MEMORYSTATUSEX msex = {sizeof(MEMORYSTATUSEX)};
if (!os_get_sys_memory_usage_internal(&msex))
return 0;
return msex.ullAvailPhys;
}
static inline bool os_get_proc_memory_usage_internal(PROCESS_MEMORY_COUNTERS *pmc)
{
if (!GetProcessMemoryInfo(GetCurrentProcess(), pmc, sizeof(*pmc)))
return false;
return true;
}
bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage)
{
PROCESS_MEMORY_COUNTERS pmc = {sizeof(PROCESS_MEMORY_COUNTERS)};
if (!os_get_proc_memory_usage_internal(&pmc))
return false;
usage->resident_size = pmc.WorkingSetSize;
usage->virtual_size = pmc.PagefileUsage;
return true;
}
uint64_t os_get_proc_resident_size(void)
{
PROCESS_MEMORY_COUNTERS pmc = {sizeof(PROCESS_MEMORY_COUNTERS)};
if (!os_get_proc_memory_usage_internal(&pmc))
return 0;
return pmc.WorkingSetSize;
}
uint64_t os_get_proc_virtual_size(void)
{
PROCESS_MEMORY_COUNTERS pmc = {sizeof(PROCESS_MEMORY_COUNTERS)};
if (!os_get_proc_memory_usage_internal(&pmc))
return 0;
return pmc.PagefileUsage;
}
uint64_t os_get_free_disk_space(const char *dir)
{
wchar_t *wdir = NULL;

View file

@ -183,7 +183,8 @@ size_t os_fread_utf8(FILE *file, char **pstr)
/* remove the ghastly BOM if present */
fseek(file, 0, SEEK_SET);
fread(bom, 1, 3, file);
size_t size_read = fread(bom, 1, 3, file);
(void)size_read;
offset = (astrcmp_n(bom, "\xEF\xBB\xBF", 3) == 0) ? 3 : 0;

View file

@ -181,6 +181,18 @@ EXPORT void os_breakpoint(void);
EXPORT int os_get_physical_cores(void);
EXPORT int os_get_logical_cores(void);
EXPORT uint64_t os_get_sys_free_size(void);
struct os_proc_memory_usage {
uint64_t resident_size;
uint64_t virtual_size;
};
typedef struct os_proc_memory_usage os_proc_memory_usage_t;
EXPORT bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage);
EXPORT uint64_t os_get_proc_resident_size(void);
EXPORT uint64_t os_get_proc_virtual_size(void);
#ifdef _MSC_VER
#define strtoll _strtoi64
#if _MSC_VER < 1900

View file

@ -260,13 +260,8 @@ static bool enabled = false;
static pthread_mutex_t root_mutex = PTHREAD_MUTEX_INITIALIZER;
static DARRAY(profile_root_entry) root_entries;
#ifdef _MSC_VER
static __declspec(thread) profile_call *thread_context = NULL;
static __declspec(thread) bool thread_enabled = true;
#else
static __thread profile_call *thread_context = NULL;
static __thread bool thread_enabled = true;
#endif
static THREAD_LOCAL profile_call *thread_context = NULL;
static THREAD_LOCAL bool thread_enabled = true;
void profiler_start(void)
{

View file

@ -78,6 +78,12 @@ EXPORT int os_sem_wait(os_sem_t *sem);
EXPORT void os_set_thread_name(const char *name);
#ifdef _MSC_VER
#define THREAD_LOCAL __declspec(thread)
#else
#define THREAD_LOCAL __thread
#endif
#ifdef __cplusplus
}

View file

@ -0,0 +1,38 @@
/*
* Copyright (c) 2015 Hugh Bailey <obs.jim@gmail.com>
* Copyright (c) 2017 Ryan Foster <RytoEX@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include <windows.h>
#include "../c99defs.h"
#ifdef __cplusplus
extern "C" {
#endif
struct reg_dword {
LSTATUS status;
DWORD size;
DWORD return_value;
};
EXPORT void get_reg_dword(HKEY hkey, LPCWSTR sub_key, LPCWSTR value_name,
struct reg_dword *info);
#ifdef __cplusplus
}
#endif