File: MemoryUtil.h

package info (click to toggle)
abyss 2.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,284 kB
  • sloc: cpp: 78,182; ansic: 6,512; makefile: 2,252; perl: 672; sh: 509; haskell: 412; python: 4
file content (44 lines) | stat: -rw-r--r-- 1,191 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#ifndef MEMORYUTIL_H
#define MEMORYUTIL_H 1

#include "config.h"
#include <cassert>
#include <fstream>
#include <unistd.h> // for sbrk

#if __MACH__
# ifdef __APPLE__
#  include <mach/mach.h> // for mach_task_self
#  include <mach/task.h> // for task_info
# else
extern "C" {
#  include <mach/mach.h> // for mach_task_self and task_info
}
# endif
#endif

/** Return the number of bytes used by the data and stack segments.
 * @return -1 on error
 */
static inline ssize_t getMemoryUsage()
{
#if __MACH__
    struct task_basic_info t_info;
    mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
	int status = task_info(mach_task_self(),
			TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
	assert(status == KERN_SUCCESS);
	return status == KERN_SUCCESS ? (ssize_t)t_info.virtual_size : -1;
#elif HAVE_GETPAGESIZE
	std::ifstream in("/proc/self/statm");
	size_t size, resident, share, text, lib, data;
	return in >> size >> resident >> share >> text >> lib >> data
		? ssize_t(data * getpagesize()) : -1;
#else
	/** Start of the data segment. */
	static intptr_t sbrk0 = reinterpret_cast<intptr_t>(sbrk(0));
	return reinterpret_cast<intptr_t>(sbrk(0)) - sbrk0;
#endif
}

#endif