Repository navigation
Expand file tree
/
Copy patharray_client.c
More file actions
156 lines (136 loc) · 5.45 KB
/
Copy patharray_client.c
File metadata and controls
156 lines (136 loc) · 5.45 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hurd.h>
#include <hurd/hurd_types.h>
#include "machoo_class.h"
#include "machoo_object.h"
static mach_port_t create_integer(mach_port_t constructor, int value)
{
mach_port_t object;
kern_return_t retVal = integerWithValue(constructor, value, &object);
if (retVal != 0) {
fprintf(stderr, "error creating integer object: %d\n", retVal);
exit(-1);
}
return object;
}
static mach_port_t create_array(mach_port_t constructor)
{
mach_port_t object;
kern_return_t retVal = machoo_create_object(constructor, &object);
if (retVal != 0) {
fprintf(stderr, "error creating array object: %d\n", retVal);
exit(-1);
}
return object;
}
int main(int argc, char *argv[])
{
mach_port_t int_class = file_name_lookup("/tmp/integer", 0, 0);
if (int_class == MACH_PORT_NULL) {
fprintf(stderr, "cannot look up /tmp/integer\n");
exit(-1);
}
mach_port_t arr_class = file_name_lookup("/tmp/array", 0, 0);
if (arr_class == MACH_PORT_NULL) {
fprintf(stderr, "cannot look up /tmp/array\n");
exit(-1);
}
// Create 3 integers - these are objects that "box" the C int type.
mach_port_t int1 = create_integer(int_class, 10);
mach_port_t int2 = create_integer(int_class, 20);
mach_port_t int3 = create_integer(int_class, 30);
// Create an array.
mach_port_t array = create_array(arr_class);
// Initialize the array to hold the three integers.
// The array takes copies of the send rights to those objects.
mach_port_t *objects = NULL;
vm_allocate(mach_task_self(), (vm_address_t *)&objects, 3 * sizeof(mach_port_t), TRUE);
objects[0] = int1;
objects[1] = int2;
objects[2] = int3;
mach_port_t reply = MACH_PORT_NULL;
kern_return_t err1 = machoo_msg_send(array, "initWithObjects:", objects, MACH_MSG_TYPE_COPY_SEND, 3, &reply);
if (err1 != ERR_SUCCESS) {
fprintf(stderr, "initWithObjects error: %d\n", err1);
}
if (reply != MACH_PORT_NULL) {
mach_port_deallocate(mach_task_self(), reply);
}
// Send the count message, which returns a boxed int reporting the number of objects in the array.
mach_port_t count_obj = MACH_PORT_NULL;
kern_return_t err2 = machoo_msg_send(array, "count", NULL, MACH_MSG_TYPE_COPY_SEND, 0, &count_obj);
if (err2 != ERR_SUCCESS) {
fprintf(stderr, "count error: %d\n", err2);
}
// Unbox the count.
if (count_obj != MACH_PORT_NULL) {
int count = -1;
integerValue(count_obj, &count);
printf("Array count: %d\n", count);
mach_port_deallocate(mach_task_self(), count_obj);
}
// Get the object at index 1 (the second object in the array).
mach_port_t index_obj = create_integer(int_class, 1);
mach_port_t item_obj = MACH_PORT_NULL;
mach_port_t *index_args = NULL;
vm_allocate(mach_task_self(), (vm_address_t *)&index_args, sizeof(mach_port_t), TRUE);
index_args[0] = index_obj;
kern_return_t err3 = machoo_msg_send(array, "objectAtIndex:", index_args, MACH_MSG_TYPE_COPY_SEND, 1, &item_obj);
if (err3 != ERR_SUCCESS) {
fprintf(stderr, "objectAtIndex:1 error: %d\n", err3);
}
if (item_obj != MACH_PORT_NULL) {
int val = -1;
integerValue(item_obj, &val);
printf("Item at index 1: %d\n", val);
mach_port_deallocate(mach_task_self(), item_obj);
} else {
printf("Item at index 1: NULL\n");
}
// Get the object at index 5, which is out of the array's bounds.
// The array should reply with a NULL port.
mach_port_t out_index_obj = create_integer(int_class, 5);
mach_port_t out_item_obj = MACH_PORT_NULL;
mach_port_t *out_index_args = NULL;
vm_allocate(mach_task_self(), (vm_address_t *)&out_index_args, sizeof(mach_port_t), TRUE);
out_index_args[0] = out_index_obj;
kern_return_t err4 = machoo_msg_send(array, "objectAtIndex:", out_index_args, MACH_MSG_TYPE_COPY_SEND, 1, &out_item_obj);
if (err4 != ERR_SUCCESS) {
fprintf(stderr, "objectAtIndex:5 error: %d\n", err4);
}
if (out_item_obj != MACH_PORT_NULL) {
int val = -1;
integerValue(out_item_obj, &val);
printf("Item at index 5: %d\n", val);
mach_port_deallocate(mach_task_self(), out_item_obj);
} else {
printf("Item at index 5: NULL\n");
}
// The empty array is a "flyweight" instance, so the two ports we get back should be identical.
mach_port_t empty_array1 = create_array(arr_class);
mach_port_t empty_array2 = create_array(arr_class);
mach_port_t reply_empty1 = MACH_PORT_NULL;
kern_return_t err_ea1 = machoo_msg_send(empty_array1, "initWithObjects:", NULL, MACH_MSG_TYPE_COPY_SEND, 0, &reply_empty1);
mach_port_t reply_empty2 = MACH_PORT_NULL;
kern_return_t err_ea2 = machoo_msg_send(empty_array2, "initWithObjects:", NULL, MACH_MSG_TYPE_COPY_SEND, 0, &reply_empty2);
if (err_ea1 == ERR_SUCCESS && err_ea2 == ERR_SUCCESS) {
if (reply_empty1 == reply_empty2) {
printf("Empty arrays are flyweighted! (port %u == %u)\n", reply_empty1, reply_empty2);
} else {
printf("Empty arrays are NOT flyweighted! (port %u != %u)\n", reply_empty1, reply_empty2);
}
} else {
printf("Error in empty arrays: err_ea1=%d, err_ea2=%d\n", err_ea1, err_ea2);
}
mach_port_deallocate(mach_task_self(), index_obj);
mach_port_deallocate(mach_task_self(), out_index_obj);
mach_port_deallocate(mach_task_self(), int1);
mach_port_deallocate(mach_task_self(), int2);
mach_port_deallocate(mach_task_self(), int3);
mach_port_deallocate(mach_task_self(), array);
mach_port_deallocate(mach_task_self(), int_class);
mach_port_deallocate(mach_task_self(), arr_class);
return 0;
}