cleanup warnings in gcc, fixed some hw module cleanup routines
[dcpu16] / hw_spc2000.c
index 2fa0c7b3ff8702734681adec556ad7316aed099b..49ba35042c5ace4360c7eca649617c52b5cecb20 100644 (file)
@@ -78,3 +78,39 @@ void spc2000_hwi_(struct dcpu16 *vm, void *data) {
             break;
     }
 }
+
+struct dcpu16_hw *spc2000_new(struct dcpu16 *vm) {
+    struct dcpu16_hw *hw;
+
+    hw = calloc(1, sizeof *hw);
+    if (hw == NULL) {
+        vm->warn_cb_("%s():%s", "calloc", strerror(errno));
+        return NULL;
+    }
+
+    memcpy(hw, &hw_, sizeof *hw);
+
+    hw->data = calloc(1, sizeof(struct spc2000_));
+    if (hw->data == NULL) {
+        vm->warn_cb_("%s():%s", "calloc", strerror(errno));
+        free(hw);
+        return NULL;
+    }
+
+    hw->vm = vm;
+
+    return hw;
+}
+
+void spc2000_del(struct dcpu16_hw **hw) {
+    if (hw) {
+        if (*hw) {
+            if ((*hw)->data) {
+                free((*hw)->data);
+                (*hw)->data = NULL;
+            }
+            free(*hw);
+            *hw = NULL;
+        }
+    }
+}