0
votes

I keep on getting this error when running my code,

ping: malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.

My code is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ucontext.h>
#include "mythread.h"
#define STACK 1024*64

ucontext_t init_thr, new_thr;
static int thread_id = 1; 
struct _MyThreadStruct
{
    ucontext_t ctxt;
    int thr_id;
    int states; // 1-Ready, 2-Running, 3-Blocked, 4-Blocked by all, 5-Exit
    struct _MyThreadStruct *parent;
    //struct threadList *children;  
};

typedef struct _MyThreadStruct * _MyThread;
_MyThread currentthread;

struct threadList
{
    _MyThread thr;
    struct threadList *link;
};

struct threadList *front,*rear,*temp,*front1;

_MyThread frontelement();
void enq(_MyThread thr);
void deq();
void empty();
void create();
void queuesize();

int qSize = 0;

MyThread MyThreadCreate(void(*start_funct)(void *), void *args)
{
    getcontext(&new_thr);
    new_thr.uc_link = &currentthread->ctxt;
    new_thr.uc_stack.ss_sp = malloc(STACK);
    new_thr.uc_stack.ss_size = STACK;
    new_thr.uc_stack.ss_flags = 0;
    makecontext(&new_thr, (void (*)(void)) start_funct,1,args);
    _MyThread newthread = (_MyThread )malloc(sizeof(_MyThread));
    newthread->ctxt = new_thr;
    newthread->thr_id = thread_id++;
    newthread->states = 1;
    newthread->parent = currentthread;
    //newthread->children = (struct threadList)malloc(sizeof(struct threadList));
    enq(newthread);
    return (MyThread)newthread;
}
void MyThreadYield()
{
    enq(currentthread);
    currentthread = frontelement();
    deq();
    queuesize();
    setcontext(&currentthread->ctxt);
}
void MyThreadExit()
{
    printf("in Exit");  
}
/* Create an empty queue */
void create()
{
    front = rear = NULL;
}

/* Returns queue size */
void queuesize()
{
    printf("\nQueue size : %d", qSize);
}

/* Enqueing the queue */
void enq(_MyThread thr)
{
    if (rear == NULL)
    {
        rear = (struct threadList *)malloc(1*sizeof(struct threadList));
        rear->link = NULL;
        rear->thr = thr;
        front = rear;
    }
    else
    {
        temp = (struct threadList *)malloc(1*sizeof(struct threadList));
        rear->link = temp;
        temp->thr = thr;
        temp->link = NULL;
        rear = temp;
    }
    qSize++;
}

/* Dequeing the queue */
void deq()
{
    front1 = front; 
    if (front1 == NULL)
    {
        printf("\nNo threads on queue");
        return;
    }
    else
    if (front1->link != NULL)
    {
        front1 = front1->link;
        printf("\nDequed");
        free(front);
        front = front1;
    }
    else
    {
        printf("\nDequed");
        free(front);
        front = NULL;
        rear = NULL;
    }
    qSize--;
}

/* Returns the front element of queue */
_MyThread frontelement()
{
    if ((front != NULL) && (rear != NULL))
        return(front->thr);
    else
        printf("\nQueue empty");
}

/* Display if queue is empty or not */
void empty()
{
    if ((front == NULL) && (rear == NULL))
        printf("\nQueue empty");
    else
        printf("\nQueue not empty");
}

void MyThreadInit (void(*start_funct)(void *), void *args)
{
    getcontext(&init_thr);
    init_thr.uc_link = 0;
    init_thr.uc_stack.ss_sp = malloc(STACK);
    init_thr.uc_stack.ss_size = STACK;
    init_thr.uc_stack.ss_flags = 0;
    makecontext(&init_thr, (void (*)(void)) start_funct,1,args);
    printf("\n00000");
    _MyThread initthread = (_MyThread)malloc(sizeof(_MyThread));
    printf("\n11111");
    initthread->ctxt = init_thr;
    printf("\n22222");
    initthread->thr_id = thread_id;
    initthread->states = 2;
    initthread->parent = NULL;
    //initthread->children = (struct threadList)malloc(sizeof(struct threadList));  
    create();
    currentthread = initthread;
    setcontext(&initthread->ctxt);
}

I'm calling the MyThreadInit from a separate piece of code. And it fails at

_MyThread initthread = (_MyThread)malloc(sizeof(_MyThread));

Can someone please help me on getting it fixed?

1
should be _MyThread initthread = (_MyThread)malloc(sizeof(*initthread)); - BLUEPIXY
Tried that. Doesn't work. :( - user4021353
Problem will be present also in the other. TL;DR - BLUEPIXY
typedef struct _MyThreadStruct * _MyThread; here _MyThread is structure pointer and you can't use it's size in malloc. Instead you have to declare it like typedef struct _MyThreadStruct _MyThread; then you can use it like sizeof(_MyThread) in malloc. - Jayesh Bhoi
Yeah. I just figured that out. Thank you. - user4021353

1 Answers

1
votes

I had error while using the typedef _MyThread in malloc. I used struct _MyThreadStruct instead and it works fine. Need to look into the typedef thing.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ucontext.h>
#include "mythread.h"
#define STACK 1024*64

ucontext_t init_thr, new_thr;
static int thread_id = 1; 
struct _MyThread
{
    ucontext_t ctxt;
    int thr_id;
    int states; // 1-Ready, 2-Running, 3-Blocked, 4-Blocked by all, 5-Exit
    struct _MyThread *parent;
    //struct threadList *children;  
};

struct _MyThread * currentthread;

struct threadList
{
    struct _MyThread *thr;
    struct threadList *link;
};

struct threadList *front,*rear,*temp,*front1;

struct _MyThread * frontelement();
void enq(struct _MyThread * thr);
void deq();
void empty();
void create();
void queuesize();

int qSize = 0;

MyThread MyThreadCreate(void(*start_funct)(void *), void *args)
{
    getcontext(&new_thr);
    new_thr.uc_link = &currentthread->ctxt;
    new_thr.uc_stack.ss_sp = malloc(STACK);
    new_thr.uc_stack.ss_size = STACK;
    new_thr.uc_stack.ss_flags = 0;
    makecontext(&new_thr, (void (*)(void)) start_funct,1,args);
    struct _MyThread * newthread = (struct _MyThread *)malloc(sizeof(struct _MyThread));
    newthread->ctxt = new_thr;
    newthread->thr_id = thread_id++;
    newthread->states = 1;
    newthread->parent = currentthread;
    //newthread->children = (struct threadList)malloc(sizeof(struct threadList));
    enq(newthread);
    return (MyThread)newthread;
}
void MyThreadYield()
{
    enq(currentthread);
    currentthread = frontelement();
    deq();
    queuesize();
    setcontext(&currentthread->ctxt);
}
void MyThreadExit()
{
    printf("in Exit");  
}
/* Create an empty queue */
void create()
{
    front = rear = NULL;
}

/* Returns queue size */
void queuesize()
{
    printf("\nQueue size : %d", qSize);
}

/* Enqueing the queue */
void enq(struct _MyThread * thr)
{
    if (rear == NULL)
    {
        rear = (struct threadList *)malloc(1*sizeof(struct threadList));
        rear->link = NULL;
        rear->thr = thr;
        front = rear;
    }
    else
    {
        temp = (struct threadList *)malloc(1*sizeof(struct threadList));
        rear->link = temp;
        temp->thr = thr;
        temp->link = NULL;
        rear = temp;
    }
    qSize++;
}

/* Dequeing the queue */
void deq()
{
    front1 = front; 
    if (front1 == NULL)
    {
        printf("\nNo threads on queue");
        return;
    }
    else
    if (front1->link != NULL)
    {
        front1 = front1->link;
        printf("\nDequed");
        free(front);
        front = front1;
    }
    else
    {
        printf("\nDequed");
        free(front);
        front = NULL;
        rear = NULL;
    }
    qSize--;
}

/* Returns the front element of queue */
struct _MyThread * frontelement()
{
    if ((front != NULL) && (rear != NULL))
        return(front->thr);
    else
        printf("\nQueue empty");
}

/* Display if queue is empty or not */
void empty()
{
    if ((front == NULL) && (rear == NULL))
        printf("\nQueue empty");
    else
        printf("\nQueue not empty");
}

void MyThreadInit (void(*start_funct)(void *), void *args)
{
    getcontext(&init_thr);
    init_thr.uc_link = 0;
    init_thr.uc_stack.ss_sp = malloc(STACK);
    init_thr.uc_stack.ss_size = STACK;
    init_thr.uc_stack.ss_flags = 0;
    makecontext(&init_thr, (void (*)(void)) start_funct,1,args);
    struct _MyThread * initthread = (struct _MyThread *)malloc(sizeof(struct _MyThread));
    initthread->ctxt = init_thr;
    initthread->thr_id = thread_id;
    initthread->states = 2;
    initthread->parent = NULL;
    //initthread->children = (struct threadList)malloc(sizeof(struct threadList));  
    create();
    currentthread = initthread;
    setcontext(&initthread->ctxt);
}