diff --git a/bsd/memory_freebsd.cc b/bsd/memory_freebsd.cc index cf86e96..23fe67f 100644 --- a/bsd/memory_freebsd.cc +++ b/bsd/memory_freebsd.cc @@ -26,7 +26,7 @@ #include "getsysctl.h" #include "memory.h" #include "../luts.h" -#include "../config.h" +#include "../conversions.h" std::string mem_string( bool use_colors = false ) { @@ -63,7 +63,8 @@ std::string mem_string( bool use_colors = false ) oss << mem_lut[( 100 * used_mem ) / total_mem]; } - oss << MEGABYTES( used_mem ) << '/' << MEGABYTES( total_mem ) << "MB"; + oss << convert_unit( used_mem, MEGABYTES ) << '/' + << convert_unit( total_mem, MEGABYTES ) << "MB"; if( use_colors ) { diff --git a/bsd/memory_openbsd.cc b/bsd/memory_openbsd.cc index 5de361e..69bdd03 100644 --- a/bsd/memory_openbsd.cc +++ b/bsd/memory_openbsd.cc @@ -29,7 +29,7 @@ #include "getsysctl.h" #include "memory.h" #include "../luts.h" -#include "../config.h" +#include "../conversions.h" std::string mem_string( bool use_colors = false ) { @@ -65,7 +65,8 @@ std::string mem_string( bool use_colors = false ) oss << mem_lut[( 100 * used_mem ) / total_mem]; } - oss << MEGABYTES( used_mem ) << '/' << MEGABYTES( total_mem ) << "MB"; + oss << convert_unit( used_mem, MEGABYTES ) << '/' + << convert_unit( total_mem, MEGABYTES ) << "MB"; if( use_colors ) { diff --git a/config.h.in b/config.h.in index 10a77b9..5268d15 100644 --- a/config.h.in +++ b/config.h.in @@ -22,7 +22,3 @@ #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/conversions.h b/conversions.h new file mode 100644 index 0000000..701f177 --- /dev/null +++ b/conversions.h @@ -0,0 +1,35 @@ +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * + * Copyright 2012 Matthew McCormick + * Copyright 2015 Pawel 'l0ner' Soltys + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +enum +{ + BYTES = 0, + KILOBYTES = 1, + MEGABYTES = 2, + GIGABYTES = 3 +}; + +template +inline T convert_unit( T num, int to, int from = BYTES) +{ + for(from; from < to; from++) + { + num /= 1024; + } + return num; +} diff --git a/linux/memory.cc b/linux/memory.cc index 591310b..9f96b0d 100644 --- a/linux/memory.cc +++ b/linux/memory.cc @@ -22,7 +22,7 @@ #include "memory.h" #include "../luts.h" -#include "../config.h" +#include "../conversions.h" std::string mem_string( bool use_colors = false ) { @@ -92,7 +92,8 @@ std::string mem_string( bool use_colors = false ) // we want megabytes on output, but since the values already come as // kilobytes we need to divide them by 1024 only once, thus we use // KILOBYTES - oss << KILOBYTES(used_mem) << '/' << KILOBYTES(total_mem) << "MB"; + oss << convert_unit(used_mem, MEGABYTES, KILOBYTES) << '/' + << convert_unit(total_mem, MEGABYTES, KILOBYTES) << "MB"; if( use_colors ) { diff --git a/osx/memory.cc b/osx/memory.cc index f9ac677..bacad9d 100644 --- a/osx/memory.cc +++ b/osx/memory.cc @@ -23,7 +23,7 @@ #include "memory.h" #include "../luts.h" -#include "../config.h" +#include "../conversions.h" std::string mem_string( bool use_colors ) { @@ -64,7 +64,8 @@ std::string mem_string( bool use_colors ) oss << mem_lut[( 100 * used_mem ) / total_mem]; } - oss << MEGABYTES(used_mem) << '/' << MEGABYTES(total_mem) << "MB"; + oss << convert_unit( used_mem, MEGABYTES ) << '/' + << convert_unit( total_mem, MEGABYTES ) << "MB"; if( use_colors ) {