<commit_before>#include <std.h>
#include <debug.h>
#include <vt100.h>
#include <scheduler.h>

#include <tasks/name_server.h>
#include <syscall.h>

/*
 * All of the internal structs required by the name server implmentation
 */
typedef enum {
    REGISTER,
    LOOKUP,
    REVERSE
} ns_req_type;

typedef union {
    char text[NAME_MAX_SIZE];
    int  overlay[NAME_OVERLAY_SIZE];
} ns_payload;

typedef struct {
    ns_req_type type;
    ns_payload  payload;
} ns_req;

typedef struct {
    union {
        char text[NAME_MAX][NAME_MAX_SIZE];
    	int  overlay[NAME_MAX][NAME_OVERLAY_SIZE];
    }   lookup;
    int lookup_insert;
    int tasks[NAME_MAX];
} ns_context;

/*
 * Static data
 */
static int name_server_tid;
#ifdef ASSERT
static ns_context* _context;
#endif

/*
 * look up functions
 */
static int __attribute__((const))
find_loc(int directory[][NAME_OVERLAY_SIZE],
         const int value[NAME_OVERLAY_SIZE],
         const int stored) {

    for (int i = 0; i < stored; i++) {
	for (int j = 0;;j++) {
	    if(j == NAME_OVERLAY_SIZE) return i;
            if(directory[i][j] != value[j]) break;
	}
    }
    return -1;
}

static int __attribute__((const))
find_name(const int dir[NAME_MAX], const int tid, const int stored) {

    for (int i = 0; i < stored; i++) {
        if (dir[i] == tid) { return i; }
    }
    return -1;
}

static inline void register_tid(ns_context* ctxt,
				const int tid,
				const ns_payload* const data) {
    int reply = 0;
    int loc   = find_loc(ctxt->lookup.overlay,
                         data->overlay,
                         ctxt->lookup_insert);

    if(loc < 0) {
        if(ctxt->lookup_insert < NAME_MAX) {
	    ctxt->lookup.overlay[ctxt->lookup_insert][0] = data->overlay[0];
	    ctxt->lookup.overlay[ctxt->lookup_insert][1] = data->overlay[1];
	    ctxt->tasks[ctxt->lookup_insert++] = tid;
	} else {
            reply = -69;
	}
    } else {
        ctxt->tasks[loc] = tid;
    }

    Reply(tid, (char*)&reply, sizeof(reply));
}

static inline int lookup_tid(ns_context* ctxt,
			     const ns_payload* const data) {
    int loc = find_loc(ctxt->lookup.overlay,
                       data->overlay,
                       ctxt->lookup_insert);
    return loc < 0 ? -69 : ctxt->tasks[loc];
}

static inline char* lookup_name(ns_context* const ctxt,
				const ns_payload* const data) {
    int loc = find_name(ctxt->tasks, data->overlay[0], ctxt->lookup_insert);
    return loc < 0 ? NULL : ctxt->lookup.text[loc];
}

void name_server() {
    int        tid;
    ns_req     buffer;
    ns_context context;

    #ifdef ASSERT
    _context = &context;
    #endif

    name_server_tid = myTid();
<commit_msg>Initialize the name server state so that it works correctly<commit_after>#include <std.h>
#include <debug.h>
#include <vt100.h>
#include <scheduler.h>

#include <tasks/name_server.h>
#include <syscall.h>

/*
 * All of the internal structs required by the name server implmentation
 */
typedef enum {
    REGISTER,
    LOOKUP,
    REVERSE
} ns_req_type;

typedef union {
    char text[NAME_MAX_SIZE];
    int  overlay[NAME_OVERLAY_SIZE];
} ns_payload;

typedef struct {
    ns_req_type type;
    ns_payload  payload;
} ns_req;

typedef struct {
    union {
        char text[NAME_MAX][NAME_MAX_SIZE];
    	int  overlay[NAME_MAX][NAME_OVERLAY_SIZE];
    }   lookup;
    int lookup_insert;
    int tasks[NAME_MAX];
} ns_context;

/*
 * Static data
 */
static int name_server_tid;
#ifdef ASSERT
static ns_context* _context;
#endif

/*
 * look up functions
 */
static int __attribute__((const))
find_loc(int directory[][NAME_OVERLAY_SIZE],
         const int value[NAME_OVERLAY_SIZE],
         const int stored) {

    for (int i = 0; i < stored; i++) {
	for (int j = 0;;j++) {
	    if(j == NAME_OVERLAY_SIZE) return i;
            if(directory[i][j] != value[j]) break;
	}
    }
    return -1;
}

static int __attribute__((const))
find_name(const int dir[NAME_MAX], const int tid, const int stored) {

    for (int i = 0; i < stored; i++) {
        if (dir[i] == tid) { return i; }
    }
    return -1;
}

static inline void register_tid(ns_context* ctxt,
				const int tid,
				const ns_payload* const data) {
    int reply = 0;
    int loc   = find_loc(ctxt->lookup.overlay,
                         data->overlay,
                         ctxt->lookup_insert);

    if(loc < 0) {
        if(ctxt->lookup_insert < NAME_MAX) {
	    ctxt->lookup.overlay[ctxt->lookup_insert][0] = data->overlay[0];
	    ctxt->lookup.overlay[ctxt->lookup_insert][1] = data->overlay[1];
	    ctxt->tasks[ctxt->lookup_insert++] = tid;
	} else {
            reply = -69;
	}
    } else {
        ctxt->tasks[loc] = tid;
    }

    Reply(tid, (char*)&reply, sizeof(reply));
}

static inline int lookup_tid(ns_context* ctxt,
			     const ns_payload* const data) {
    int loc = find_loc(ctxt->lookup.overlay,
                       data->overlay,
                       ctxt->lookup_insert);
    return loc < 0 ? -69 : ctxt->tasks[loc];
}

static inline char* lookup_name(ns_context* const ctxt,
				const ns_payload* const data) {
    int loc = find_name(ctxt->tasks, data->overlay[0], ctxt->lookup_insert);
    return loc < 0 ? NULL : ctxt->lookup.text[loc];
}

void name_server() {
    int        tid;
    ns_req     buffer;
    ns_context context;
    memset(&context, 0, sizeof(context));

    #ifdef ASSERT
    _context = &context;
    #endif

    name_server_tid = myTid();

{'fraction_non_alphanumeric': 0.12952029520295202, 'fraction_numerical': 0.007933579335793358, 'mean_word_length': 2.655428186109238, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 57, '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': '1843095', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1845, 'n_words': 452}