/
descriptor-System.UInt16.mm
139 lines (123 loc) · 6 KB
/
descriptor-System.UInt16.mm
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
//
// This file is part of Monobjc, a .NET/Objective-C bridge
// Copyright (C) 2007-2013 - Laurent Etiemble
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
/**
* \file descriptor-System.UInt16.mm
* @brief Contains the descriptor code to handle the System.UInt16 type.
* \author Laurent Etiemble
* \date 2009-2010
*/
#include "logging.h"
#include "marshal.h"
/**
* @brief Allocates a storage zone large enough to hold a marshalled System.UInt16.
* @param descriptor The descriptor.
* @param native TRUE if the storage zone has the native type width (direct access)
* or FALSE if the zone is at least the size of pointer storage (as FFI requires it).
* @return A pointer to the allocated storage.
*
* @remark The storage zone is at least the size of pointer storage, due to libffi requirement.
*/
static void *__alloc_native_storage_for_System_UInt16(MonobjcTypeDescriptor *descriptor, boolean_t native) {
//LOG_DEBUG(MONOBJC_DOMAIN_MARSHALLING, "__alloc_native_storage_for_System_UInt16()");
if (native) {
// Allocate a storage zone
uint16_t *ptr = g_new(uint16_t, 1);
return ptr;
} else {
// Allocate a storage zone
uint32_t *ptr = g_new(uint32_t, 1);
return ptr;
}
}
/**
* @brief Marshal a native value into a System.UInt16 object.
* @param descriptor The descriptor.
* @param ptr The pointer to the storage zone that contains the native object.
* @param native TRUE if the storage zone has the native type width (direct access)
* or FALSE if the zone is at least the size of pointer storage (as FFI requires it).
* @return A fully initialized System.UInt16 object.
*
* @remark This function only use the storage zone. It is up to the caller to free it.
* Due to libffi requirement, the storage zone used is at least the size of pointer storage.
* This mean that smaller types are promoted before being boxed.
*/
static MonoObject *__marshal_from_native_for_System_UInt16(MonobjcTypeDescriptor *descriptor, void *ptr, boolean_t native) {
//LOG_DEBUG(MONOBJC_DOMAIN_MARSHALLING, "__marshal_from_native_for_System_UInt16(%p)", ptr);
if (native) {
// Promote the value and box it into a managed object
uint16_t value = *((uint16_t *) ptr);
MonoObject *obj = mono_value_box(mono_domain_get(), mono_get_uint16_class(), &value);
return obj;
} else {
// Promote the value and box it into a managed object
uint32_t value = *((uint32_t *) ptr);
uint16_t promoted = (uint16_t) value;
MonoObject *obj = mono_value_box(mono_domain_get(), mono_get_uint16_class(), &promoted);
return obj;
}
}
/**
* @brief Marshal a System.UInt16 object to its native value.
* @param descriptor The descriptor.
* @param obj The managed object.
* @param ptr The pointer to the storage zone that will contain the native object.
* @param native TRUE if the storage zone has the native type width (direct access)
* or FALSE if the zone is at least the size of pointer storage (as FFI requires it).
*
* @remark This function does not allocate the storage zone. It is up to the caller to allocate it and free it.
*/
static void __marshal_to_native_for_System_UInt16(MonobjcTypeDescriptor *descriptor, MonoObject *obj, void *ptr, boolean_t native) {
//LOG_DEBUG(MONOBJC_DOMAIN_MARSHALLING, "__marshal_to_native_for_System_UInt16()");
// Copy the value directly from the unboxed result
uint16_t value = *((uint16_t *) mono_object_unbox(obj));
*(uint16_t *)ptr = value;
}
/**
* @brief Free the previously allocated storage zone.
* @param ptr The pointer to the storage zone.
*/
static void __free_native_storage_for_System_UInt16(void *ptr) {
//LOG_DEBUG(MONOBJC_DOMAIN_MARSHALLING, "__free_native_storage_for_System_UInt16(%p)", ptr);
// Free the storage zone
g_free(ptr);
}
MonobjcTypeDescriptor *monobjc_create_descriptor_for_System_UInt16() {
LOG_DEBUG(MONOBJC_DOMAIN_MARSHALLING, "monobjc_create_descriptor_for_System_UInt16");
// Allocate the descriptor structure
MonobjcTypeDescriptor *descriptor = g_new0(MonobjcTypeDescriptor, 1);
// Set the type
descriptor->type = mono_class_get_type(mono_get_uint16_class());
descriptor->boxed = 1;
// Set the various marshaling functions
descriptor->alloc_native_storage = __alloc_native_storage_for_System_UInt16;
descriptor->marshal_from_native = __marshal_from_native_for_System_UInt16;
descriptor->marshal_to_native = __marshal_to_native_for_System_UInt16;
descriptor->free_native_storage = __free_native_storage_for_System_UInt16;
// Set the Objective-C encoding
descriptor->encoding = strdup("S");
descriptor->size = sizeof(uint16_t);
descriptor->alignment = MIN(log2(descriptor->size), log2(sizeof(void *)));
// Set the type to use with libffi
descriptor->foreign_type = &ffi_type_uint16;
return descriptor;
}