mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-05-05 06:59:32 +03:00
fix path max in realpath on unix (pr #69)
This commit is contained in:
parent
8203f3dcfa
commit
76023ca45f
2 changed files with 27 additions and 7 deletions
23
src/alloc.c
23
src/alloc.c
|
@ -454,19 +454,30 @@ char* mi_heap_realpath(mi_heap_t* heap, const char* fname, char* resolved_name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
#include <limits.h>
|
#include <unistd.h>
|
||||||
#ifndef PATH_MAX
|
static size_t mi_path_max() {
|
||||||
#define PATH_MAX 260
|
static size_t path_max = 0;
|
||||||
#endif
|
if (path_max <= 0) {
|
||||||
|
long m = pathconf("/",_PC_PATH_MAX);
|
||||||
|
if (m <= 0) path_max = 4096; // guess
|
||||||
|
else if (m < 256) path_max = 256; // at least 256
|
||||||
|
else path_max = m;
|
||||||
|
}
|
||||||
|
return path_max;
|
||||||
|
}
|
||||||
|
|
||||||
char* mi_heap_realpath(mi_heap_t* heap, const char* fname, char* resolved_name) mi_attr_noexcept {
|
char* mi_heap_realpath(mi_heap_t* heap, const char* fname, char* resolved_name) mi_attr_noexcept {
|
||||||
if (resolved_name != NULL) {
|
if (resolved_name != NULL) {
|
||||||
return realpath(fname,resolved_name);
|
return realpath(fname,resolved_name);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
char buf[PATH_MAX+1];
|
size_t n = mi_path_max();
|
||||||
|
char* buf = (char*)mi_malloc(n+1);
|
||||||
|
if (buf==NULL) return NULL;
|
||||||
char* rname = realpath(fname,buf);
|
char* rname = realpath(fname,buf);
|
||||||
return mi_heap_strndup(heap,rname,PATH_MAX); // ok if `rname==NULL`
|
char* result = mi_heap_strndup(heap,rname,n); // ok if `rname==NULL`
|
||||||
|
mi_free(buf);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -139,6 +139,15 @@ int main() {
|
||||||
CHECK("heap_destroy", test_heap1());
|
CHECK("heap_destroy", test_heap1());
|
||||||
CHECK("heap_delete", test_heap2());
|
CHECK("heap_delete", test_heap2());
|
||||||
|
|
||||||
|
// ---------------------------------------------------
|
||||||
|
// various
|
||||||
|
// ---------------------------------------------------
|
||||||
|
CHECK_BODY("realpath", {
|
||||||
|
char* s = mi_realpath( ".", NULL );
|
||||||
|
// printf("realpath: %s\n",s);
|
||||||
|
mi_free(s);
|
||||||
|
});
|
||||||
|
|
||||||
// ---------------------------------------------------
|
// ---------------------------------------------------
|
||||||
// Done
|
// Done
|
||||||
// ---------------------------------------------------[]
|
// ---------------------------------------------------[]
|
||||||
|
|
Loading…
Add table
Reference in a new issue