From d3eb9e3187b53c6304904f8ff71113d9b8875ece Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Sun, 18 Jan 2015 17:21:19 +0100 Subject: [PATCH] use macros from config.h to calculate megabyes instead of using hard coded divisions to calculate ram stats it's better to use macros defined in config.h. BSD port was doing this already, using macros defined in it's common.h header. I pulled those macros out and applied them to all platforms. File version.h.in got renamed into config.h.in since it doesn't caontain only the version anymore. --- CMakeLists.txt | 2 +- bsd/common.h | 5 ----- bsd/memory_freebsd.cc | 1 + bsd/memory_openbsd.cc | 1 + version.h.in => config.h.in | 5 +++++ linux/memory.cc | 9 +++++---- osx/memory.cc | 7 ++----- tmux-mem-cpu-load.cpp | 2 +- 8 files changed, 16 insertions(+), 16 deletions(-) rename version.h.in => config.h.in (88%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a229c2..4e42569 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,7 +46,7 @@ endif() # generate header file to handle version configure_file( - "${PROJECT_SOURCE_DIR}/version.h.in" "${PROJECT_SOURCE_DIR}/version.h" + "${PROJECT_SOURCE_DIR}/config.h.in" "${PROJECT_SOURCE_DIR}/config.h" ) # set buold type diff --git a/bsd/common.h b/bsd/common.h index fb0ff7f..58f6860 100644 --- a/bsd/common.h +++ b/bsd/common.h @@ -27,11 +27,6 @@ #include #include -// Memory Sizes -#define KILOBYTES(x) ((x)/1024) -#define MEGABYTES(x) (KILOBYTES((x))/1024) -#define GIGABYTES(x) (MEGABYTES((x))/1024) - // CPU percentages stuff #define CP_USER 0 #define CP_NICE 1 diff --git a/bsd/memory_freebsd.cc b/bsd/memory_freebsd.cc index 50d3e11..d37f8b4 100644 --- a/bsd/memory_freebsd.cc +++ b/bsd/memory_freebsd.cc @@ -26,6 +26,7 @@ #include "common.h" #include "memory.h" #include "../luts.h" +#include "../config.h" std::string mem_string( bool use_colors = false ) { diff --git a/bsd/memory_openbsd.cc b/bsd/memory_openbsd.cc index 84e29b8..ba00c6b 100644 --- a/bsd/memory_openbsd.cc +++ b/bsd/memory_openbsd.cc @@ -29,6 +29,7 @@ #include "common.h" #include "memory.h" #include "../luts.h" +#include "../config.h" std::string mem_string( bool use_colors = false ) { diff --git a/version.h.in b/config.h.in similarity index 88% rename from version.h.in rename to config.h.in index c46dd04..10a77b9 100644 --- a/version.h.in +++ b/config.h.in @@ -21,3 +21,8 @@ #define tmux_mem_cpu_load_VERSION_MINOR @tmux-mem-cpu-load_VERSION_MINOR@ #define tmux_mem_cpu_load_VERSION_PATCH @tmux-mem-cpu-load_VERSION_PATCH@ #define tmux_mem_cpu_load_VERSION "@tmux-mem-cpu-load_VERSION@" + +// Memory Sizes +#define KILOBYTES(x) ((x)/1024) +#define MEGABYTES(x) (KILOBYTES((x))/1024) +#define GIGABYTES(x) (MEGABYTES((x))/1024) diff --git a/linux/memory.cc b/linux/memory.cc index 42580a5..7254c5b 100644 --- a/linux/memory.cc +++ b/linux/memory.cc @@ -21,6 +21,7 @@ #include "memory.h" #include "../luts.h" +#include "../config.h" std::string mem_string( bool use_colors = false ) { @@ -29,17 +30,17 @@ std::string mem_string( bool use_colors = false ) struct sysinfo sinfo; sysinfo(&sinfo); - unsigned int total_mem = sinfo.totalram / 1014; - unsigned int used_mem = total_mem - sinfo.freeram / 1024; + unsigned int total_mem = MEGABYTES(sinfo.totalram); + unsigned int used_mem = total_mem - MEGABYTES(sinfo.freeram); // we don't need this for now - //unsigned int unused_mem = sinfo.freeram / 1024; + //unsigned int unused_mem = MEGABYTES(sinfo.freeram); if( use_colors ) { oss << mem_lut[(100 * used_mem) / total_mem]; } - oss << used_mem / 1024 << '/' << total_mem / 1024 << "MB"; + oss << used_mem << '/' << total_mem << "MB"; if( use_colors ) { diff --git a/osx/memory.cc b/osx/memory.cc index d83e5be..f9ac677 100644 --- a/osx/memory.cc +++ b/osx/memory.cc @@ -23,6 +23,7 @@ #include "memory.h" #include "../luts.h" +#include "../config.h" std::string mem_string( bool use_colors ) { @@ -58,16 +59,12 @@ std::string mem_string( bool use_colors ) ) * ( int64_t )page_size; } - // To kilobytes - used_mem /= 1024; - total_mem /= 1024; - if( use_colors ) { oss << mem_lut[( 100 * used_mem ) / total_mem]; } - oss << used_mem / 1024 << '/' << total_mem / 1024 << "MB"; + oss << MEGABYTES(used_mem) << '/' << MEGABYTES(total_mem) << "MB"; if( use_colors ) { diff --git a/tmux-mem-cpu-load.cpp b/tmux-mem-cpu-load.cpp index 15d090f..7f8f312 100644 --- a/tmux-mem-cpu-load.cpp +++ b/tmux-mem-cpu-load.cpp @@ -23,7 +23,7 @@ #include // EXIT_SUCCESS #include "argParse/argParse.h" -#include "version.h" +#include "config.h" #include "graph.h" // Tmux color lookup tables for the different metrics.