summaryrefslogtreecommitdiff
path: root/comphelper/source/misc/meminfo.cxx
blob: 762d4ef1a9b2291f4a8404e14fabcacb56f41ba3 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you 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 .
 */

#include <sal/types.h>
#include <comphelper/meminfo.hxx>

#if defined(_WIN32)

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "psapi.h"

#elif defined(UNX)

#if defined(MACOSX)
#include <sys/types.h>
#include <mach/mach.h>
#include <mach/host_info.h>
#include <mach/mach_host.h>

#else // Linux
#include <fstream>
#include <unistd.h>
#endif

#endif

namespace comphelper
{
// Returns the virtual memory used currently by the process.
static sal_Int64 getMemUsedBySelf()
{
#if defined(UNX)
#if defined(MACOSX)

    vm_size_t pageSize;
    struct task_basic_info tInfo;
    mach_msg_type_number_t tInfoCount = TASK_BASIC_INFO_COUNT;

    if (host_page_size(mach_host_self(), &pageSize) != KERN_SUCCESS)
        return -1;

    if (task_info(mach_task_self(), TASK_BASIC_INFO, reinterpret_cast<task_info_t>(&tInfo),
                  &tInfoCount)
        != KERN_SUCCESS)
        return -1;

    return static_cast<sal_Int64>(tInfo.virtual_size) * pageSize;

#elif defined(LINUX)

    sal_Int64 pageSize = sysconf(_SC_PAGESIZE);
    if (pageSize <= 0)
        return -1;

    std::ifstream ifs("/proc/self/statm", std::ifstream::in);
    if (!ifs.is_open())
        return -1;

    sal_Int64 vmSize;
    if (ifs >> vmSize)
        return vmSize * pageSize;

    return -1;

#else

    return -1;

#endif

#elif defined(_WIN32)

    PROCESS_MEMORY_COUNTERS_EX pmc;
    GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc),
                         sizeof(pmc));
    return static_cast<sal_Int64>(pmc.PrivateUsage);

#else

    return -1;

#endif
}

static sal_Int64 getMaxAddressableMemLimit()
{
#if defined(_WIN64)
    return 8796093022208ll; // 8 TB
#elif defined(_WIN32)
    return 2147483648ll; // 2 GB
#elif defined(__x86_64__)
    // TODO: check for artificial limits imposed by 'ulimit -Sv' too for UNX ?
    return 140737488355328ll; // 128 TB
#elif defined(__i386__)
    return 3221225472ll; // 3 GB
#else
    return 2147483648ll; // 2 GB
#endif
}

// canAlloc() checks whether allocSize bytes can be allocated without exceeding
// addressable memory limit. This is useful in case of 32-bit systems where
// process addressable memory limit is less than physical memory limit.
bool canAlloc(sal_Int64 allocSize)
{
    if (allocSize <= 0)
        return true;

    sal_Int64 vmSize = getMemUsedBySelf();
    if (vmSize < 0)
        return true;

    sal_Int64 maxSize = getMaxAddressableMemLimit();

    if (vmSize + allocSize <= maxSize)
        return true;

    return false;
}

} // namespace comphelper

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */