Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Change return type of uv_get_*_memory() functions
... from double to uint64_t. Limit use of floating point in public API as much
as possible.
  • Loading branch information
bnoordhuis committed Oct 21, 2011
1 parent 9757a43 commit d396799
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 37 deletions.
4 changes: 2 additions & 2 deletions include/uv.h
Expand Up @@ -1127,8 +1127,8 @@ int uv_ip6_name(struct sockaddr_in6* src, char* dst, size_t size);
int uv_exepath(char* buffer, size_t* size);

/* Gets memory info in bytes */
double uv_get_free_memory(void);
double uv_get_total_memory(void);
uint64_t uv_get_free_memory(void);
uint64_t uv_get_total_memory(void);

/*
* Returns the current high-resolution real time. This is expressed in
Expand Down
8 changes: 4 additions & 4 deletions src/unix/cygwin.c
Expand Up @@ -58,12 +58,12 @@ int uv_exepath(char* buffer, size_t* size) {
return 0;
}

double uv_get_free_memory(void) {
return (double) sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES);
uint64_t uv_get_free_memory(void) {
return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES);
}

double uv_get_total_memory(void) {
return (double) sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES);
uint64_t uv_get_total_memory(void) {
return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES);
}

int uv_fs_event_init(uv_loop_t* loop,
Expand Down
8 changes: 4 additions & 4 deletions src/unix/darwin.c
Expand Up @@ -71,7 +71,7 @@ int uv_exepath(char* buffer, size_t* size) {
return 0;
}

double uv_get_free_memory(void) {
uint64_t uv_get_free_memory(void) {
vm_statistics_data_t info;
mach_msg_type_number_t count = sizeof(info) / sizeof(integer_t);

Expand All @@ -80,10 +80,10 @@ double uv_get_free_memory(void) {
return -1;
}

return (double) info.free_count * sysconf(_SC_PAGESIZE);
return (uint64_t) info.free_count * sysconf(_SC_PAGESIZE);
}

double uv_get_total_memory(void) {
uint64_t uv_get_total_memory(void) {
uint64_t info;
int which[] = {CTL_HW, HW_MEMSIZE};
size_t size = sizeof(info);
Expand All @@ -92,7 +92,7 @@ double uv_get_total_memory(void) {
return -1;
}

return (double) info;
return (uint64_t) info;
}

void uv_loadavg(double avg[3]) {
Expand Down
8 changes: 4 additions & 4 deletions src/unix/freebsd.c
Expand Up @@ -70,19 +70,19 @@ int uv_exepath(char* buffer, size_t* size) {
return 0;
}

double uv_get_free_memory(void) {
uint64_t uv_get_free_memory(void) {
int freecount;
size_t size = sizeof(freecount);

if(sysctlbyname("vm.stats.vm.v_free_count",
&freecount, &size, NULL, 0) == -1){
return -1;
}
return (double) freecount * sysconf(_SC_PAGESIZE);
return (uint64_t) freecount * sysconf(_SC_PAGESIZE);

}

double uv_get_total_memory(void) {
uint64_t uv_get_total_memory(void) {
unsigned long info;
int which[] = {CTL_HW, HW_PHYSMEM};

Expand All @@ -92,7 +92,7 @@ double uv_get_total_memory(void) {
return -1;
}

return (double) info;
return (uint64_t) info;
}

void uv_loadavg(double avg[3]) {
Expand Down
8 changes: 4 additions & 4 deletions src/unix/linux.c
Expand Up @@ -75,12 +75,12 @@ int uv_exepath(char* buffer, size_t* size) {
return 0;
}

double uv_get_free_memory(void) {
return (double) sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES);
uint64_t uv_get_free_memory(void) {
return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES);
}

double uv_get_total_memory(void) {
return (double) sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES);
uint64_t uv_get_total_memory(void) {
return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES);
}

static int new_inotify_fd(void) {
Expand Down
8 changes: 4 additions & 4 deletions src/unix/netbsd.c
Expand Up @@ -82,7 +82,7 @@ int uv_exepath(char* buffer, size_t* size) {
return 0;
}

double uv_get_free_memory(void) {
uint64_t uv_get_free_memory(void) {
struct uvmexp info;
size_t size = sizeof(info);
int which[] = {CTL_VM, VM_UVMEXP};
Expand All @@ -91,10 +91,10 @@ double uv_get_free_memory(void) {
return -1;
}

return (double) info.free * psysconf(_SC_PAGESIZE);
return (uint64_t) info.free * psysconf(_SC_PAGESIZE);
}

double uv_get_total_memory(void) {
uint64_t uv_get_total_memory(void) {
#if defined(HW_PHYSMEM64)
uint64_t info;
int which[] = {CTL_HW, HW_PHYSMEM64};
Expand All @@ -108,5 +108,5 @@ double uv_get_total_memory(void) {
return -1;
}

return (double) info;
return (uint64_t) info;
}
8 changes: 4 additions & 4 deletions src/unix/openbsd.c
Expand Up @@ -98,7 +98,7 @@ int uv_exepath(char* buffer, size_t* size) {
return status;
}

double uv_get_free_memory(void) {
uint64_t uv_get_free_memory(void) {
struct uvmexp info;
size_t size = sizeof(info);
int which[] = {CTL_VM, VM_UVMEXP};
Expand All @@ -107,10 +107,10 @@ double uv_get_free_memory(void) {
return -1;
}

return (double) info.free * sysconf(_SC_PAGESIZE);
return (uint64_t) info.free * sysconf(_SC_PAGESIZE);
}

double uv_get_total_memory(void) {
uint64_t uv_get_total_memory(void) {
uint64_t info;
int which[] = {CTL_HW, HW_PHYSMEM64};
size_t size = sizeof(info);
Expand All @@ -119,5 +119,5 @@ double uv_get_total_memory(void) {
return -1;
}

return (double) info;
return (uint64_t) info;
}
8 changes: 4 additions & 4 deletions src/unix/sunos.c
Expand Up @@ -66,13 +66,13 @@ int uv_exepath(char* buffer, size_t* size) {
}


double uv_get_free_memory(void) {
return (double) sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES);
uint64_t uv_get_free_memory(void) {
return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES);
}


double uv_get_total_memory(void) {
return (double) sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES);
uint64_t uv_get_total_memory(void) {
return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES);
}


Expand Down
8 changes: 4 additions & 4 deletions src/win/util.c
Expand Up @@ -103,7 +103,7 @@ void uv_loadavg(double avg[3]) {
}


double uv_get_free_memory(void) {
uint64_t uv_get_free_memory(void) {
MEMORYSTATUSEX memory_status;
memory_status.dwLength = sizeof(memory_status);

Expand All @@ -112,11 +112,11 @@ double uv_get_free_memory(void) {
return -1;
}

return (double)memory_status.ullAvailPhys;
return (uint64_t)memory_status.ullAvailPhys;
}


double uv_get_total_memory(void) {
uint64_t uv_get_total_memory(void) {
MEMORYSTATUSEX memory_status;
memory_status.dwLength = sizeof(memory_status);

Expand All @@ -125,7 +125,7 @@ double uv_get_total_memory(void) {
return -1;
}

return (double)memory_status.ullTotalPhys;
return (uint64_t)memory_status.ullTotalPhys;
}


Expand Down
6 changes: 3 additions & 3 deletions test/test-get-memory.c
Expand Up @@ -23,10 +23,10 @@
#include "task.h"

TEST_IMPL(get_memory) {
double free_mem = uv_get_free_memory();
double total_mem = uv_get_total_memory();
uint64_t free_mem = uv_get_free_memory();
uint64_t total_mem = uv_get_total_memory();

printf("free_mem=%.0f, total_mem=%.0f\n", free_mem, total_mem);
printf("free_mem=%llu, total_mem=%llu\n", free_mem, total_mem);

ASSERT(free_mem > 0);
ASSERT(total_mem > 0);
Expand Down

0 comments on commit d396799

Please sign in to comment.