~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to src/pkg/runtime/windows/mem.c

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-04-20 17:36:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110420173648-ifergoxyrm832trd
Tags: upstream-2011.03.07.1
Import upstream version 2011.03.07.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2010 The Go Authors.  All rights reserved.
 
2
// Use of this source code is governed by a BSD-style
 
3
// license that can be found in the LICENSE file.
 
4
 
 
5
#include "runtime.h"
 
6
#include "os.h"
 
7
#include "defs.h"
 
8
#include "malloc.h"
 
9
 
 
10
enum {
 
11
        MEM_COMMIT = 0x1000,
 
12
        MEM_RESERVE = 0x2000,
 
13
        MEM_RELEASE = 0x8000,
 
14
        
 
15
        PAGE_EXECUTE_READWRITE = 0x40,
 
16
};
 
17
 
 
18
#pragma dynimport runtime·VirtualAlloc VirtualAlloc "kernel32.dll"
 
19
#pragma dynimport runtime·VirtualFree VirtualFree "kernel32.dll"
 
20
extern void *runtime·VirtualAlloc;
 
21
extern void *runtime·VirtualFree;
 
22
 
 
23
void*
 
24
runtime·SysAlloc(uintptr n)
 
25
{
 
26
        mstats.sys += n;
 
27
        return runtime·stdcall(runtime·VirtualAlloc, 4, nil, n, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
 
28
}
 
29
 
 
30
void
 
31
runtime·SysUnused(void *v, uintptr n)
 
32
{
 
33
        USED(v);
 
34
        USED(n);
 
35
}
 
36
 
 
37
void
 
38
runtime·SysFree(void *v, uintptr n)
 
39
{
 
40
        uintptr r;
 
41
 
 
42
        mstats.sys -= n;
 
43
        r = (uintptr)runtime·stdcall(runtime·VirtualFree, 3, v, 0, MEM_RELEASE);
 
44
        if(r == 0)
 
45
                runtime·throw("runtime: failed to release pages");
 
46
}
 
47
 
 
48
void*
 
49
runtime·SysReserve(void *v, uintptr n)
 
50
{
 
51
        // v is just a hint.
 
52
        // First try at v.
 
53
        v = runtime·stdcall(runtime·VirtualAlloc, 4, v, n, MEM_RESERVE, PAGE_EXECUTE_READWRITE);
 
54
        if(v != nil)
 
55
                return v;
 
56
        
 
57
        // Next let the kernel choose the address.
 
58
        return runtime·stdcall(runtime·VirtualAlloc, 4, nil, n, MEM_RESERVE, PAGE_EXECUTE_READWRITE);
 
59
}
 
60
 
 
61
void
 
62
runtime·SysMap(void *v, uintptr n)
 
63
{
 
64
        void *p;
 
65
        
 
66
        mstats.sys += n;
 
67
        p = runtime·stdcall(runtime·VirtualAlloc, 4, v, n, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
 
68
        if(p != v)
 
69
                runtime·throw("runtime: cannot map pages in arena address space");
 
70
}