<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "dmt.h"

typedef struct dmt_node_t {
  struct dmt_node_t *prev, *next;
  const char *file;
  size_t line;
  size_t size;
} dmt_node_t;


dmt_node_t *dmt_head;



int _dmt_has_node(dmt_node_t *n) {
  dmt_node_t *node = dmt_head;
  while (node != NULL) {
    if (node == n) return 1;
    node = node->next;
  }
  return 0;
}



void *_dmt_alloc(size_t sz, const char *file, unsigned line, int zeroset) {
  dmt_node_t *node = NULL;
  
  if (zeroset) {
    node = calloc(sizeof(*node) + sz, 1);
  } else {
    node = malloc(sizeof(*node) + sz);
    memset(node, 0, sizeof(*node));
  }

  node->line = line;
  node->file = file;
  node->size = sz;

  if (dmt_head) {
    dmt_head->prev = node;
    node->next = dmt_head;
  }
  dmt_head = node;

  return (char*)node + sizeof(*node);
}



void *_dmt_realloc(void *ptr, size_t sz, const char *file, unsigned line) {
  dmt_node_t *node = (dmt_node_t*)((char*)ptr - sizeof(*node));

  if (!_dmt_has_node(node)) {
    printf("Bad realloc: %p %s, line %u\n", ptr, file, line);
    abort();
  }

  node = realloc(node, sz);
  node->size = sz;
  if (node->prev) node->prev->next = node;
  if (node->next) node->next->prev = node;

  return node;
}



void _dmt_free(void *ptr, const char *file, unsigned line) {
  dmt_node_t *node = (dmt_node_t*)((char*)ptr - sizeof(*node));

  if (!_dmt_has_node(node)) {
    printf("Bad free: %p %s, line %u\n", ptr, file, line);
    abort();<commit_msg>Fixed null ptr handling in _dmt_realloc/_dmt_free<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "dmt.h"

typedef struct dmt_node_t {
  struct dmt_node_t *prev, *next;
  const char *file;
  size_t line;
  size_t size;
} dmt_node_t;


dmt_node_t *dmt_head;



int _dmt_has_node(dmt_node_t *n) {
  dmt_node_t *node = dmt_head;
  while (node != NULL) {
    if (node == n) return 1;
    node = node->next;
  }
  return 0;
}



void *_dmt_alloc(size_t sz, const char *file, unsigned line, int zeroset) {
  dmt_node_t *node = NULL;
  
  if (zeroset) {
    node = calloc(sizeof(*node) + sz, 1);
  } else {
    node = malloc(sizeof(*node) + sz);
    memset(node, 0, sizeof(*node));
  }

  node->line = line;
  node->file = file;
  node->size = sz;

  if (dmt_head) {
    dmt_head->prev = node;
    node->next = dmt_head;
  }
  dmt_head = node;

  return (char*)node + sizeof(*node);
}



void *_dmt_realloc(void *ptr, size_t sz, const char *file, unsigned line) {
  dmt_node_t *node = (dmt_node_t*)((char*)ptr - sizeof(*node));

  if (ptr == NULL) return _dmt_alloc(sz, file, line, 0);

  if (!_dmt_has_node(node)) {
    printf("Bad realloc: %p %s, line %u\n", ptr, file, line);
    abort();
  }

  node = realloc(node, sz);
  node->size = sz;
  if (node->prev) node->prev->next = node;
  if (node->next) node->next->prev = node;

  return node;
}



void _dmt_free(void *ptr, const char *file, unsigned line) {
  dmt_node_t *node = (dmt_node_t*)((char*)ptr - sizeof(*node));

  if (ptr == NULL) return;

  if (!_dmt_has_node(node)) {
    printf("Bad free: %p %s, line %u\n", ptr, file, line);
    abort();
{'fraction_non_alphanumeric': 0.16857506361323155, 'fraction_numerical': 0.0028625954198473282, 'mean_word_length': 3.4109396914446, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '861075', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1302, 'n_words': 318}