PageRenderTime 17ms CodeModel.GetById 9ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/red/runtime/imports.reds

http://github.com/dockimbel/Red
Unknown | 129 lines | 117 code | 12 blank | 0 comment | 0 complexity | d40871b5dc4e93fd06fd24eb97d3250a MD5 | raw file
  1Red/System [
  2	Title:   "Red runtime OS API imported functions definitions"
  3	Author:  "Nenad Rakocevic"
  4	File: 	 %imports.reds
  5	Rights:  "Copyright (C) 2011 Nenad Rakocevic. All rights reserved."
  6	License: {
  7		Distributed under the Boost Software License, Version 1.0.
  8		See https://github.com/dockimbel/Red/blob/master/red-system/runtime/BSL-License.txt
  9	}
 10]
 11
 12#define OS-page-size	4096					;@@ target/OS dependent
 13
 14#either OS = 'Windows [
 15	#import [
 16		"kernel32.dll" stdcall [
 17			OS-VirtualAlloc: "VirtualAlloc" [
 18				address		[byte-ptr!]
 19				size		[integer!]
 20				type		[integer!]
 21				protection	[integer!]
 22				return:		[int-ptr!]
 23			]
 24			OS-VirtualFree: "VirtualFree" [
 25				address 	[int-ptr!]
 26				size		[integer!]
 27				return:		[integer!]
 28			]
 29		]
 30	]
 31	
 32	#define VA_COMMIT_RESERVE	3000h			;-- MEM_COMMIT | MEM_RESERVE
 33	#define VA_PAGE_RW			04h				;-- PAGE_READWRITE
 34	#define VA_PAGE_RWX			40h				;-- PAGE_EXECUTE_READWRITE
 35	
 36	;-------------------------------------------
 37	;-- Allocate paged virtual memory region from OS (Windows)
 38	;-------------------------------------------
 39	OS-allocate-virtual: func [
 40		size 	[integer!]						;-- allocated size in bytes (page size multiple)
 41		exec? 	[logic!]						;-- TRUE => executable region
 42		return: [int-ptr!]						;-- allocated memory region pointer
 43		/local ptr prot
 44	][
 45		prot: either exec? [VA_PAGE_RWX][VA_PAGE_RW]
 46		
 47		ptr: OS-VirtualAlloc 
 48			null
 49			size
 50			VA_COMMIT_RESERVE
 51			prot
 52			
 53		if ptr = null [
 54			raise-error RED_ERR_VMEM_OUT_OF_MEMORY 0
 55		]
 56		ptr
 57	]
 58	
 59	;-------------------------------------------
 60	;-- Free paged virtual memory region from OS (Windows)
 61	;-------------------------------------------
 62	OS-free-virtual: func [
 63		ptr [int-ptr!]							;-- address of memory region to release
 64	][
 65		if negative? OS-VirtualFree ptr ptr/value [
 66			raise-error RED_ERR_VMEM_RELEASE_FAILED as-integer ptr
 67		]
 68	]
 69][	
 70	#define MMAP_PROT_RW		03h				;-- PROT_READ | PROT_WRITE
 71	#define MMAP_PROT_RWX		07h				;-- PROT_READ | PROT_WRITE | PROT_EXEC
 72	
 73	#define MMAP_MAP_SHARED     01h
 74	#define MMAP_MAP_PRIVATE    02h
 75	#define MMAP_MAP_ANONYMOUS  20h
 76
 77	#syscall [
 78		OS-mmap: SYSCALL_MMAP2 [
 79			address		[byte-ptr!]
 80			size		[integer!]
 81			protection	[integer!]
 82			flags		[integer!]
 83			fd			[integer!]
 84			offset		[integer!]
 85			return:		[byte-ptr!]
 86		]
 87		OS-munmap: SYSCALL_MUNMAP [
 88			address		[byte-ptr!]
 89			size		[integer!]
 90			return:		[integer!]
 91		]
 92	]
 93	;-------------------------------------------
 94	;-- Allocate paged virtual memory region from OS (UNIX)
 95	;-------------------------------------------
 96	OS-allocate-virtual: func [
 97		size 	[integer!]						;-- allocated size in bytes (page size multiple)
 98		exec? 	[logic!]						;-- TRUE => executable region
 99		return: [int-ptr!]						;-- allocated memory region pointer
100		/local ptr prot
101	][
102		assert zero? (size and 0Fh)				;-- size is a multiple of 16
103		prot: either exec? [MMAP_PROT_RWX][MMAP_PROT_RW]
104		
105		ptr: OS-mmap 
106			null 
107			size
108			prot	
109			MMAP_MAP_PRIVATE or MMAP_MAP_ANONYMOUS
110			-1									;-- portable value
111			0
112			
113		if negative? as-integer ptr [
114			raise-error RED_ERR_VMEM_OUT_OF_MEMORY as-integer system/pc
115		]
116		as int-ptr! ptr
117	]
118	
119	;-------------------------------------------
120	;-- Free paged virtual memory region from OS (UNIX)
121	;-------------------------------------------	
122	OS-free-virtual: func [
123		ptr [int-ptr!]							;-- address of memory region to release
124	][
125		if negative? OS-munmap as byte-ptr! ptr ptr/value [
126			raise-error RED_ERR_VMEM_RELEASE_FAILED as-integer system/pc
127		]
128	]
129]