+#define ALIGN_INT(val,alignment) (((val) + (alignment - 1)) & ~(alignment - 1))
+
+#if defined(__native_client_codegen__) && defined(__native_client__)
+/* End of text segment, set by linker.
+ * Dynamic text starts on the next allocated page.
+ */
+extern char etext[];
+char *next_dynamic_code_addr = NULL;
+
+/*
+ * This routine gets the next available bundle aligned
+ * pointer in the dynamic code section. It does not check
+ * for the section end, this error will be caught in the
+ * service runtime.
+ */
+void*
+allocate_code(intptr_t increment)
+{
+ char *addr;
+ if (increment < 0) return NULL;
+ increment = increment & kNaClBundleMask ? (increment & ~kNaClBundleMask) + kNaClBundleSize : increment;
+ addr = next_dynamic_code_addr;
+ next_dynamic_code_addr += increment;
+ return addr;
+}
+
+int
+nacl_is_code_address (void *target)
+{
+ return (char *)target < next_dynamic_code_addr;
+}
+
+const int kMaxPatchDepth = 32;
+__thread unsigned char **patch_source_base = NULL;
+__thread unsigned char **patch_dest_base = NULL;
+__thread int *patch_alloc_size = NULL;
+__thread int patch_current_depth = -1;
+__thread int allow_target_modification = 1;
+
+void
+nacl_allow_target_modification (int val)
+{
+ allow_target_modification = val;
+}
+
+static void
+nacl_jit_check_init ()
+{
+ if (patch_source_base == NULL) {
+ patch_source_base = g_malloc (kMaxPatchDepth * sizeof(unsigned char *));
+ patch_dest_base = g_malloc (kMaxPatchDepth * sizeof(unsigned char *));
+ patch_alloc_size = g_malloc (kMaxPatchDepth * sizeof(int));
+ }
+}
+
+
+/* Given a patch target, modify the target such that patching will work when
+ * the code is copied to the data section.
+ */
+void*
+nacl_modify_patch_target (unsigned char *target)
+{
+ /* This seems like a bit of an ugly way to do this but the advantage
+ * is we don't have to worry about all the conditions in
+ * mono_resolve_patch_target, and it can be used by all the bare uses
+ * of <arch>_patch.
+ */
+ unsigned char *sb;
+ unsigned char *db;
+
+ if (!allow_target_modification) return target;
+
+ nacl_jit_check_init ();
+ sb = patch_source_base[patch_current_depth];
+ db = patch_dest_base[patch_current_depth];
+
+ if (target >= sb && (target < sb + patch_alloc_size[patch_current_depth])) {
+ /* Do nothing. target is in the section being generated.
+ * no need to modify, the disp will be the same either way.
+ */
+ } else {
+ int target_offset = target - db;
+ target = sb + target_offset;
+ }
+ return target;
+}
+
+void*
+nacl_inverse_modify_patch_target (unsigned char *target)
+{
+ unsigned char *sb;
+ unsigned char *db;
+ int target_offset;
+
+ if (!allow_target_modification) return target;
+
+ nacl_jit_check_init ();
+ sb = patch_source_base[patch_current_depth];
+ db = patch_dest_base[patch_current_depth];
+
+ target_offset = target - sb;
+ target = db + target_offset;
+ return target;
+}
+
+
+#endif /* __native_client_codegen && __native_client__ */
+
+/**
+ * mono_code_manager_new:
+ *
+ * Creates a new code manager. A code manager can be used to allocate memory
+ * suitable for storing native code that can be later executed.
+ * A code manager allocates memory from the operating system in large chunks
+ * (typically 64KB in size) so that many methods can be allocated inside them
+ * close together, improving cache locality.
+ *
+ * Returns: the new code manager
+ */