Exemplo n.º 1
0
/* Handle the RX ring */
static int dev_pos_oc3_handle_rxring(netio_desc_t *nio,
                                     u_char *pkt,ssize_t pkt_len,
                                     struct pos_oc3_data *d)
{
#if DEBUG_RECEIVE
   POS_LOG(d,"receiving a packet of %d bytes\n",pkt_len);
   mem_dump(log_file,pkt,pkt_len);
#endif

   dev_pos_oc3_receive_pkt(d,pkt,pkt_len);
   return(TRUE);
}
Exemplo n.º 2
0
/**
 * dump a proc file with `entry_size` byte entries to an existing chunk of
 * memory.
 */
uint64_t proc_dump(int out_fd, char *proc_file, uint64_t entry_size)
{
    int proc_fd;
    struct stat proc_stat;
    uint64_t proc_size;
    uint64_t log_size;
    void *proc_addr;

    /* get the size of the proc file */
    if (stat(proc_file, &proc_stat) != 0) {
        perror("stat proc_file");
        return 0;
    }
    proc_size = proc_stat.st_size;

    /* attempt to proc_open file */
    proc_fd = open(proc_file, O_RDONLY);
    if (proc_fd < 0) {
        if (errno == EACCES) {
            /* EACCES means the file was not used */
            /* we can just skip this round */
            return 0;
        }
        perror("open proc_file");
        return 0;
    }

    /* mmap() for access */
    proc_addr = mmap(NULL, proc_size, PROT_READ, MAP_SHARED, proc_fd, 0);
    if (proc_addr == MAP_FAILED) {
        perror("mmap");
        close(proc_fd);
        return 0;
    }

    log_size = mem_dump(out_fd, proc_addr, proc_size, entry_size);

    /* unmmap() for access */
    if (munmap(proc_addr, proc_size) == -1) {
        perror("munmap");
    }

    /* close file */
    close(proc_fd);

    return log_size;
}
Exemplo n.º 3
0
int main(int argc, char *argv[]) {
    srand(time(NULL));
    read_args(argc, argv);

    alc_init(amount);
    printf("\n");
    mem_dump();
    printf("\n");

    if (test) {
        test_mem();
    }

    if (stats) {
        get_stats();
    }
}
Exemplo n.º 4
0
/* Receive a packet through a NetIO descriptor */
ssize_t netio_recv(netio_desc_t *nio,void *pkt,size_t max_len)
{
   ssize_t len;
   int res;

   if (!nio)
      return(-1);

   /* Receive the packet */
   if ((len = nio->recv(nio->dptr,pkt,max_len)) <= 0)
      return(-1);

   if (nio->debug) {
      printf("NIO %s: receiving a packet of %ld bytes:\n",nio->name,(long)len);
      mem_dump(stdout,pkt,len);
   }

   /* Apply the RX filter */
   if (nio->rx_filter != NULL) {
      res = nio->rx_filter->pkt_handler(nio,pkt,len,nio->rx_filter_data);

      if (res == NETIO_FILTER_ACTION_DROP)
         return(-1);
   }

   /* Apply the bidirectional filter */
   if (nio->both_filter != NULL) {
      res = nio->both_filter->pkt_handler(nio,pkt,len,nio->both_filter_data);

      if (res == NETIO_FILTER_ACTION_DROP)
         return(-1);
   }

   /* Update input statistics */
   nio->stats_pkts_in++;
   nio->stats_bytes_in += len;
   return(len);
}
Exemplo n.º 5
0
/* Send a packet through a NetIO descriptor */
ssize_t netio_send(netio_desc_t *nio,void *pkt,size_t len)
{
   int res;

   if (!nio)
      return(-1);

   if (nio->debug) {
      printf("NIO %s: sending a packet of %lu bytes:\n",nio->name,(u_long)len);
      mem_dump(stdout,pkt,len);
   }

   /* Apply the TX filter */
   if (nio->tx_filter != NULL) {
      res = nio->tx_filter->pkt_handler(nio,pkt,len,nio->tx_filter_data);

      if (res <= 0)
         return(-1);
   }

   /* Apply the bidirectional filter */
   if (nio->both_filter != NULL) {
      res = nio->both_filter->pkt_handler(nio,pkt,len,nio->both_filter_data);

      if (res == NETIO_FILTER_ACTION_DROP)
         return(-1);
   }

   /* Update output statistics */
   nio->stats_pkts_out++;
   nio->stats_bytes_out += len;

   netio_update_bw_stat(nio,len);

   return(nio->send(nio->dptr,pkt,len));
}
int main()
{
    int count = 1;
/**********************************************************************************************/
    printf("_________________________TEST PART %d_________________________\n", count++);

    printf("The pagesize is: %d\n", getpagesize());
    printf("The size of header_t is: %d\n", sizeof(header_t));
    printf("The aligned size of header_t is: %d\n", size_aligned(8, sizeof(header_t)));
    printf("The size of node_t is: %d\n", sizeof(node_t));
    printf("The aligned size of node_t is: %d\n", size_aligned(8, sizeof(node_t)));
    printf("\n\n");
/**********************************************************************************************/


/**********************************************************************************************/
    printf("_________________________TEST PART %d_________________________\n", count++);

    int rc;
    printf("Call: mem_init(0)\n");
    rc = mem_init(0);
    if(rc < 0) {
        printf("Failure: mem_init()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_init()\n");
    printf("\n\n");


    printf("Call: mem_init(-2048)\n");
    rc = mem_init(-2048);
    if(rc < 0) {
        printf("Failure: mem_init()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_init()\n");
    printf("\n\n");


    printf("Call: mem_init(3000)\n");
    rc = mem_init(3000);
    if(rc < 0) {
        printf("Failure: mem_init()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_init()\n");
    printf("\n\n");


    printf("Call: mem_init(1024)\n");
    rc = mem_init(1024);
    if(rc < 0) {
        printf("Failure: mem_init()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_init()\n");
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();
/**********************************************************************************************/


/**********************************************************************************************/
    printf("_________________________TEST PART %d_________________________\n", count++);

    void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6;
    printf("Call: ptr1 = mem_alloc(100, M_BESTFIT)\n");
    ptr1 = mem_alloc(100, M_BESTFIT);
    if(ptr1 == NULL) {
        printf("Failure: mem_alloc()\n");
        printf("The value of m_error: %d\n", m_error);
    } else {
        printf("Success: mem_alloc()\n");
        printf("The address: %p\n", ptr1);
        header_t *temp = (void *)ptr1 - size_aligned(8, sizeof(header_t));
        printf("The header address: %p\n", temp);
        printf("The header size: %d\n", temp->size);
        printf("The header magic: %d\n", temp->magic);
    }
    printf("\n\n");

    printf("Call: mem_dump()\n");
    mem_dump();

    printf("Call: mem_free(ptr1)\n");
    rc = mem_free(ptr1);
    if(rc < 0) {
        printf("Failure: mem_free()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_free()\n");
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: mem_free(ptr1)\n");
    rc = mem_free(ptr1);
    if(rc < 0) {
        printf("Failure: mem_free()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_free()\n");
    printf("\n\n");


    printf("Call: mem_free(NULL)\n");
    rc = mem_free(NULL);
    if(rc < 0) {
        printf("Failure: mem_free()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_free()\n");
    printf("\n\n");
/**********************************************************************************************/


/**********************************************************************************************/
    printf("_________________________TEST PART %d_________________________\n", count++);

    printf("Call: ptr1 = mem_alloc(100, M_BESTFIT)\n");
    ptr1 = mem_alloc(100, M_BESTFIT);
    if(ptr1 == NULL) {
        printf("Failure: mem_alloc()\n");
        printf("The value of m_error: %d\n", m_error);
    } else {
        printf("Success: mem_alloc()\n");
        printf("The address: %p\n", ptr1);
        header_t *temp = (void *)ptr1 - size_aligned(8, sizeof(header_t));
        printf("The header address: %p\n", temp);
        printf("The header size: %d\n", temp->size);
        printf("The header magic: %d\n", temp->magic);
    }
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: ptr2 = mem_alloc(100, M_WORSTFIT)\n");
    ptr2 = mem_alloc(100, M_WORSTFIT);
    if(ptr2 == NULL) {
        printf("Failure: mem_alloc()\n");
        printf("The value of m_error: %d\n", m_error);
    } else {
        printf("Success: mem_alloc()\n");
        printf("The address: %p\n", ptr2);
        header_t *temp = (void *)ptr2 - size_aligned(8, sizeof(header_t));
        printf("The header address: %p\n", temp);
        printf("The header size: %d\n", temp->size);
        printf("The header magic: %d\n", temp->magic);
    }
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: ptr3 = mem_alloc(100, M_FIRSTFIT)\n");
    ptr3 = mem_alloc(100, M_FIRSTFIT);
    if(ptr3 == NULL) {
        printf("Failure: mem_alloc()\n");
        printf("The value of m_error: %d\n", m_error);
    } else {
        printf("Success: mem_alloc()\n");
        printf("The address: %p\n", ptr3);
        header_t *temp = (void *)ptr3 - size_aligned(8, sizeof(header_t));
        printf("The header address: %p\n", temp);
        printf("The header size: %d\n", temp->size);
        printf("The header magic: %d\n", temp->magic);
    }
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: ptr4 = mem_alloc(100, M_WORSTFIT)\n");
    ptr4 = mem_alloc(100, M_WORSTFIT);
    if(ptr4 == NULL) {
        printf("Failure: mem_alloc()\n");
        printf("The value of m_error: %d\n", m_error);
    } else {
        printf("Success: mem_alloc()\n");
        printf("The address: %p\n", ptr4);
        header_t *temp = (void *)ptr4 - size_aligned(8, sizeof(header_t));
        printf("The header address: %p\n", temp);
        printf("The header size: %d\n", temp->size);
        printf("The header magic: %d\n", temp->magic);
    }
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();
/**********************************************************************************************/


/**********************************************************************************************/
    printf("_________________________TEST PART %d_________________________\n", count++);

    printf("Call: mem_free(ptr1)\n");
    rc = mem_free(ptr1);
    if(rc < 0) {
        printf("Failure: mem_free()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_free()\n");
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: mem_free(ptr3)\n");
    rc = mem_free(ptr3);
    if(rc < 0) {
        printf("Failure: mem_free()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_free()\n");
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: mem_free(ptr2)\n");
    rc = mem_free(ptr2);
    if(rc < 0) {
        printf("Failure: mem_free()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_free()\n");
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: mem_free(ptr4)\n");
    rc = mem_free(ptr4);
    if(rc < 0) {
        printf("Failure: mem_free()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_free()\n");
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();
/**********************************************************************************************/


/**********************************************************************************************/
    printf("_________________________TEST PART %d_________________________\n", count++);

    printf("Call: ptr1 = mem_alloc(500, M_BESTFIT)\n");
    ptr1 = mem_alloc(500, M_BESTFIT);
    if(ptr1 == NULL) {
        printf("Failure: mem_alloc()\n");
        printf("The value of m_error: %d\n", m_error);
    } else {
        printf("Success: mem_alloc()\n");
        printf("The address: %p\n", ptr1);
        header_t *temp = (void *)ptr1 - size_aligned(8, sizeof(header_t));
        printf("The header address: %p\n", temp);
        printf("The header size: %d\n", temp->size);
        printf("The header magic: %d\n", temp->magic);
    }
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: ptr2 = mem_alloc(1000, M_WORSTFIT)\n");
    ptr2 = mem_alloc(1000, M_WORSTFIT);
    if(ptr2 == NULL) {
        printf("Failure: mem_alloc()\n");
        printf("The value of m_error: %d\n", m_error);
    } else {
        printf("Success: mem_alloc()\n");
        printf("The address: %p\n", ptr2);
        header_t *temp = (void *)ptr2 - size_aligned(8, sizeof(header_t));
        printf("The header address: %p\n", temp);
        printf("The header size: %d\n", temp->size);
        printf("The header magic: %d\n", temp->magic);
    }
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: ptr3 = mem_alloc(100, M_FIRSTFIT)\n");
    ptr3 = mem_alloc(100, M_FIRSTFIT);
    if(ptr3 == NULL) {
        printf("Failure: mem_alloc()\n");
        printf("The value of m_error: %d\n", m_error);
    } else {
        printf("Success: mem_alloc()\n");
        printf("The address: %p\n", ptr3);
        header_t *temp = (void *)ptr3 - size_aligned(8, sizeof(header_t));
        printf("The header address: %p\n", temp);
        printf("The header size: %d\n", temp->size);
        printf("The header magic: %d\n", temp->magic);
    }
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: ptr4 = mem_alloc(20, M_WORSTFIT)\n");
    ptr4 = mem_alloc(20, M_WORSTFIT);
    if(ptr4 == NULL) {
        printf("Failure: mem_alloc()\n");
        printf("The value of m_error: %d\n", m_error);
    } else {
        printf("Success: mem_alloc()\n");
        printf("The address: %p\n", ptr4);
        header_t *temp = (void *)ptr4 - size_aligned(8, sizeof(header_t));
        printf("The header address: %p\n", temp);
        printf("The header size: %d\n", temp->size);
        printf("The header magic: %d\n", temp->magic);
    }
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: mem_free(ptr1)\n");
    rc = mem_free(ptr1);
    if(rc < 0) {
        printf("Failure: mem_free()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_free()\n");
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: mem_free(ptr3)\n");
    rc = mem_free(ptr3);
    if(rc < 0) {
        printf("Failure: mem_free()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_free()\n");
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: ptr1 = mem_alloc(1, M_WORSTFIT)\n");
    ptr1 = mem_alloc(1, M_WORSTFIT);
    if(ptr1 == NULL) {
        printf("Failure: mem_alloc()\n");
        printf("The value of m_error: %d\n", m_error);
    } else {
        printf("Success: mem_alloc()\n");
        printf("The address: %p\n", ptr1);
        header_t *temp = (void *)ptr1 - size_aligned(8, sizeof(header_t));
        printf("The header address: %p\n", temp);
        printf("The header size: %d\n", temp->size);
        printf("The header magic: %d\n", temp->magic);
    }
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: ptr3 = mem_alloc(80, M_BESTFIT)\n");
    ptr3 = mem_alloc(80, M_BESTFIT);
    if(ptr3 == NULL) {
        printf("Failure: mem_alloc()\n");
        printf("The value of m_error: %d\n", m_error);
    } else {
        printf("Success: mem_alloc()\n");
        printf("The address: %p\n", ptr3);
        header_t *temp = (void *)ptr3 - size_aligned(8, sizeof(header_t));
        printf("The header address: %p\n", temp);
        printf("The header size: %d\n", temp->size);
        printf("The header magic: %d\n", temp->magic);
    }
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: ptr5 = mem_alloc(3, M_BESTFIT)\n");
    ptr5 = mem_alloc(3, M_BESTFIT);
    if(ptr5 == NULL) {
        printf("Failure: mem_alloc()\n");
        printf("The value of m_error: %d\n", m_error);
    } else {
        printf("Success: mem_alloc()\n");
        printf("The address: %p\n", ptr5);
        header_t *temp = (void *)ptr5 - size_aligned(8, sizeof(header_t));
        printf("The header address: %p\n", temp);
        printf("The header size: %d\n", temp->size);
        printf("The header magic: %d\n", temp->magic);
    }
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: ptr6 = mem_alloc(450, M_BESTFIT)\n");
    ptr6 = mem_alloc(450, M_BESTFIT);
    if(ptr6 == NULL) {
        printf("Failure: mem_alloc()\n");
        printf("The value of m_error: %d\n", m_error);
    } else {
        printf("Success: mem_alloc()\n");
        printf("The address: %p\n", ptr6);
        header_t *temp = (void *)ptr6 - size_aligned(8, sizeof(header_t));
        printf("The header address: %p\n", temp);
        printf("The header size: %d\n", temp->size);
        printf("The header magic: %d\n", temp->magic);
    }
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: mem_free(ptr2)\n");
    rc = mem_free(ptr2);
    if(rc < 0) {
        printf("Failure: mem_free()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_free()\n");
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: mem_free(ptr4)\n");
    rc = mem_free(ptr4);
    if(rc < 0) {
        printf("Failure: mem_free()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_free()\n");
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: ptr2 = mem_alloc(1, M_FIRSTFIT)\n");
    ptr2 = mem_alloc(1, M_FIRSTFIT);
    if(ptr2 == NULL) {
        printf("Failure: mem_alloc()\n");
        printf("The value of m_error: %d\n", m_error);
    } else {
        printf("Success: mem_alloc()\n");
        printf("The address: %p\n", ptr2);
        header_t *temp = (void *)ptr2 - size_aligned(8, sizeof(header_t));
        printf("The header address: %p\n", temp);
        printf("The header size: %d\n", temp->size);
        printf("The header magic: %d\n", temp->magic);
    }
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: mem_free(ptr5)\n");
    rc = mem_free(ptr5);
    if(rc < 0) {
        printf("Failure: mem_free()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_free()\n");
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: mem_free(ptr1)\n");
    rc = mem_free(ptr1);
    if(rc < 0) {
        printf("Failure: mem_free()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_free()\n");
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: mem_free(ptr2)\n");
    rc = mem_free(ptr2);
    if(rc < 0) {
        printf("Failure: mem_free()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_free()\n");
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: mem_free(ptr6)\n");
    rc = mem_free(ptr6);
    if(rc < 0) {
        printf("Failure: mem_free()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_free()\n");
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();


    printf("Call: mem_free(ptr3)\n");
    rc = mem_free(ptr3);
    if(rc < 0) {
        printf("Failure: mem_free()\n");
        printf("The value of m_error: %d\n", m_error);
    } else printf("Success: mem_free()\n");
    printf("\n\n");


    printf("Call: mem_dump()\n");
    mem_dump();
/**********************************************************************************************/


    return 0;
}
/* Start TX DMA process */
static int mv64460_sdma_tx_start(struct mv64460_data *d,
                                 struct sdma_channel *chan)
{   
   u_char pkt[MV64460_MAX_PKT_SIZE],*pkt_ptr;
   struct sdma_desc txd0,ctxd,*ptxd;
   m_uint32_t tx_start,tx_current;
   m_uint32_t len,tot_len;
   int abort = FALSE;

   tx_start = tx_current = chan->sctdp;

   if (!tx_start)
      return(FALSE);

   ptxd = &txd0;
   mv64460_sdma_desc_read(d,tx_start,ptxd);

   /* If we don't own the first descriptor, we cannot transmit */
   if (!(txd0.cmd_stat & MV64460_TXDESC_OWN))
      return(FALSE);

   /* Empty packet for now */
   pkt_ptr = pkt;
   tot_len = 0;

   for(;;)
   {
      /* Copy packet data to the buffer */
      len = ptxd->buf_size & MV64460_TXDESC_BC_MASK;
      len >>= MV64460_TXDESC_BC_SHIFT;

      physmem_copy_from_vm(d->vm,pkt_ptr,ptxd->buf_ptr,len);
      pkt_ptr += len;
      tot_len += len;

      /* Clear the OWN bit if this is not the first descriptor */
      if (!(ptxd->cmd_stat & MV64460_TXDESC_F)) {
         ptxd->cmd_stat &= ~MV64460_TXDESC_OWN;
         physmem_copy_u32_to_vm(d->vm,tx_current+4,ptxd->cmd_stat);
      }

      //ptxd->buf_size &= 0xFFFF0000;
      //physmem_copy_u32_to_vm(d->vm,tx_current,ptxd->buf_size);

      tx_current = ptxd->next_ptr;

      /* Last descriptor or no more desc available ? */
      if (ptxd->cmd_stat & MV64460_TXDESC_L)
         break;

      if (!tx_current) {
         abort = TRUE;
         break;
      }

      /* Fetch the next descriptor */
      mv64460_sdma_desc_read(d,tx_current,&ctxd);
      ptxd = &ctxd;
   }

   if ((tot_len != 0) && !abort) {
#if DEBUG_SDMA
      MV64460_LOG(d,"SDMA%u: sending packet of %u bytes\n",tot_len);
      mem_dump(log_file,pkt,tot_len);
#endif
      /* send it on wire */
      mv64460_sdma_send_buffer(d,chan->id,pkt,tot_len);

      /* Signal that a TX buffer has been transmitted */
      mv64460_sdma_set_cause(d,chan->id,MV64460_SDMA_CAUSE_TXBUF0);
   }

   /* Clear the OWN flag of the first descriptor */
   txd0.cmd_stat &= ~MV64460_TXDESC_OWN;
   physmem_copy_u32_to_vm(d->vm,tx_start+4,txd0.cmd_stat);

   chan->sctdp = tx_current;

   if (abort || !tx_current) {
      mv64460_sdma_set_cause(d,chan->id,MV64460_SDMA_CAUSE_TXEND0);
      chan->sdcm &= ~MV64460_SDCMR_TXD;
   }

   /* Update interrupt status */
   mv64460_sdma_update_int_status(d);
   return(TRUE);
}
Exemplo n.º 8
0
/**
 * Kick off the daap server and wait for events.
 *
 * This starts the initial db scan, sets up the signal
 * handling, starts the webserver, then sits back and waits
 * for events, as notified by the signal handler and the
 * web interface.  These events are communicated via flags
 * in the config structure.
 *
 * \param argc count of command line arguments
 * \param argv command line argument pointers
 * \returns 0 on success, -1 otherwise
 *
 * \todo split out a ws_init and ws_start, so that the
 * web space handlers can be registered before the webserver
 * starts.
 *
 */
int main(int argc, char *argv[]) {
    int option;
    char *configfile=CONFFILE;
    WSCONFIG ws_config;
    int reload=0;
    int start_time;
    int end_time;
    int rescan_counter=0;
    int old_song_count, song_count;
    int force_non_root=0;
    int skip_initial=1;
    int kill_server=0;
    int convert_conf=0;
    char *db_type,*db_parms,*web_root,*runas, *tmp;
    char **mp3_dir_array;
    char *servername, *iface;
    char *ffid = NULL;
    int appdir = 0;
    char *perr=NULL;
    char txtrecord[255];
    void *phandle;
    char *plugindir;

    int err;
    char *apppath;

    int debuglevel=0;
    int plugins_loaded = 0;

#ifdef ALPHA_CUSTOMIZE
    char *share_path;
    pthread_t thread1;
#endif

    config.use_mdns=1;
    err_setlevel(2);

    config.foreground=0;
    while((option=getopt(argc,argv,"D:d:c:P:mfrysiuvab:Vk")) != -1) {
        switch(option) {
        case 'a':
            appdir = 1;
            break;

        case 'b':
            ffid=optarg;
            break;

        case 'd':
            debuglevel = atoi(optarg);
            err_setlevel(debuglevel);
            break;

        case 'D':
            if(err_setdebugmask(optarg)) {
                usage(argv[0]);
                exit(EXIT_FAILURE);
            }
            break;

        case 'f':
            config.foreground=1;
            err_setdest(err_getdest() | LOGDEST_STDERR);
            break;

        case 'c':
            configfile=optarg;
            break;

        case 'm':
            config.use_mdns=0;
            break;

#ifndef WIN32
        case 'P':
            os_set_pidfile(optarg);
            break;
#endif
        case 'r':
            reload=1;
            break;

        case 's':
            skip_initial=0;
            break;

        case 'y':
            force_non_root=1;
            break;

#ifdef WIN32
        case 'i':
            os_register();
            exit(EXIT_SUCCESS);
            break;

        case 'u':
            os_unregister();
            exit(EXIT_SUCCESS);
            break;
#endif
        case 'v':
            convert_conf=1;
            break;

        case 'k':
            kill_server=1;
            break;

        case 'V':
            fprintf(stderr,"Firefly Media Server: Version %s\n",VERSION);
            exit(EXIT_SUCCESS);
            break;

        default:
            usage(argv[0]);
            exit(EXIT_FAILURE);
            break;
        }
    }

    if((getuid()) && (!force_non_root) && (!convert_conf)) {
        fprintf(stderr,"You are not root.  This is almost certainly wrong.  "
                "If you are\nsure you want to do this, use the -y "
                "command-line switch\n");
        exit(EXIT_FAILURE);
    }


    if(kill_server) {
        os_signal_server(S_STOP);
        exit(0);
    }

    io_init();
    io_set_errhandler(main_io_errhandler);
    ws_set_errhandler(main_ws_errhandler);

    /* read the configfile, if specified, otherwise
     * try defaults */
    config.stats.start_time=start_time=(int)time(NULL);
    config.stop=0;

    /* set appdir first, that way config resolves relative to appdir */
    if(appdir) {
        apppath = os_apppath(argv[0]);
        DPRINTF(E_INF,L_MAIN,"Changing cwd to %s\n",apppath);
        chdir(apppath);
        free(apppath);
        configfile="mt-daapd.conf";
    }

    if(CONF_E_SUCCESS != conf_read(configfile)) {
        fprintf(stderr,"Error reading config file (%s)\n",configfile);
        exit(EXIT_FAILURE);
    }

    if(debuglevel) /* was specified, should override the config file */
        err_setlevel(debuglevel);

    if(convert_conf) {
        fprintf(stderr,"Converting config file...\n");
        if(CONF_E_SUCCESS != conf_write()) {
            fprintf(stderr,"Error writing config file.\n");
            exit(EXIT_FAILURE);
        }
        exit(EXIT_SUCCESS);
    }

    DPRINTF(E_LOG,L_MAIN,"Firefly Version %s: Starting with debuglevel %d\n",
            VERSION,err_getlevel());


    /* load plugins before we drop privs?  Maybe... let the
     * plugins do stuff they might need to */
    plugin_init();
    if((plugindir=conf_alloc_string("plugins","plugin_dir",NULL)) != NULL) {
        /* instead of specifying plugins, let's walk through the directory
         * and load each of them */
        if(!load_plugin_dir(plugindir)) {
            DPRINTF(E_LOG,L_MAIN,"Warning: Could not load plugins\n");
        } else {
            plugins_loaded = TRUE;
        }
        free(plugindir);
    }

    if(!plugins_loaded) {
        if((!load_plugin_dir("/usr/lib/firefly/plugins")) &&
           (!load_plugin_dir("/usr/lib/mt-daapd/plugins")) &&
           (!load_plugin_dir("/lib/mt-daapd/plugins")) &&
           (!load_plugin_dir("/lib/mt-daapd/plugins")) &&
           (!load_plugin_dir("/usr/local/lib/mt-daapd/plugins")) &&
           (!load_plugin_dir("/usr/local/lib/mt-daapd/plugins")) &&
           (!load_plugin_dir("/opt/share/firefly/plugins")) &&
           (!load_plugin_dir("/opt/share/mt-daapd/plugins")) &&
           (!load_plugin_dir("/opt/lib/firefly/plugins")) &&
           (!load_plugin_dir("/opt/lib/mt-daapd/plugins")) &&
           (!load_plugin_dir("plugins/.libs"))) {
            DPRINTF(E_FATAL,L_MAIN,"plugins/plugin_dir not specified\n");
        }
    }

    phandle=NULL;
    while((phandle=plugin_enum(phandle))) {
        DPRINTF(E_LOG,L_MAIN,"Plugin loaded: %s\n",plugin_get_description(phandle));
    }

    runas = conf_alloc_string("general","runas","nobody");

#ifndef WITHOUT_MDNS
    if(config.use_mdns) {
        DPRINTF(E_LOG,L_MAIN,"Starting rendezvous daemon\n");
        if(rend_init(runas)) {
            DPRINTF(E_FATAL,L_MAIN|L_REND,"Error in rend_init: %s\n",
                    strerror(errno));
        }
    }
#endif

    if(!os_init(config.foreground,runas)) {
        DPRINTF(E_LOG,L_MAIN,"Could not initialize server\n");
        os_deinit();
        exit(EXIT_FAILURE);
    }

    free(runas);

#ifdef UPNP
    upnp_init();
#endif

    /* this will require that the db be readable by the runas user */
    db_type = conf_alloc_string("general","db_type","sqlite");
    db_parms = conf_alloc_string("general","db_parms","/var/cache/mt-daapd");
    err=db_open(&perr,db_type,db_parms);

    if(err) {
        DPRINTF(E_LOG,L_MAIN|L_DB,"Error opening db: %s\n",perr);
#ifndef WITHOUT_MDNS
        if(config.use_mdns) {
            rend_stop();
        }
#endif
        os_deinit();
        exit(EXIT_FAILURE);
    }

    free(db_type);
    free(db_parms);

    /* Initialize the database before starting */
    DPRINTF(E_LOG,L_MAIN|L_DB,"Initializing database\n");
    if(db_init(reload)) {
        DPRINTF(E_FATAL,L_MAIN|L_DB,"Error in db_init: %s\n",strerror(errno));
    }

    err=db_get_song_count(&perr,&song_count);
    if(err != DB_E_SUCCESS) {
        DPRINTF(E_FATAL,L_MISC,"Error getting song count: %s\n",perr);
    }
    /* do a full reload if the db is empty */
    if(!song_count)
        reload = 1;

    if(conf_get_array("general","mp3_dir",&mp3_dir_array)) {
        if((!skip_initial) || (reload)) {
            DPRINTF(E_LOG,L_MAIN|L_SCAN,"Starting mp3 scan\n");
	#ifdef ALPHA_CUSTOMIZE
	    share_path = conf_alloc_string("general", "mp3_dir", "AAA");
		printf("mp3_dir_array[0] = [%s]\n", mp3_dir_array[0]);
	    if (strlen(share_path) > 0)
	    {
		Cnt_total_file(mp3_dir_array[0]);
		pthread_create( &thread1, NULL, (void*)process_bar, NULL);
		free(share_path);
	    }
	#endif
            plugin_event_dispatch(PLUGIN_EVENT_FULLSCAN_START,0,NULL,0);
            start_time=(int) time(NULL);
            if(scan_init(mp3_dir_array)) {
                DPRINTF(E_LOG,L_MAIN|L_SCAN,"Error scanning MP3 files: %s\n",strerror(errno));
            }
            if(!config.stop) { /* don't send popup when shutting down */
                plugin_event_dispatch(PLUGIN_EVENT_FULLSCAN_END,0,NULL,0);
                err=db_get_song_count(&perr,&song_count);
                end_time=(int) time(NULL);
                DPRINTF(E_LOG,L_MAIN|L_SCAN,"Scanned %d songs in %d seconds\n",
                        song_count,end_time - start_time);
            }
        }
        conf_dispose_array(mp3_dir_array);
    }
    
#ifdef ALPHA_CUSTOMIZE
    thread_exit = 1;
#endif

    /* start up the web server */
    web_root = conf_alloc_string("general","web_root",NULL);
    ws_config.web_root=web_root;
    ws_config.port=conf_get_int("general","port",0);

    DPRINTF(E_LOG,L_MAIN|L_WS,"Starting web server from %s on port %d\n",
            ws_config.web_root, ws_config.port);

    config.server=ws_init(&ws_config);
    if(!config.server) {
        /* pthreads or malloc error */
        DPRINTF(E_FATAL,L_MAIN|L_WS,"Error initializing web server\n");
    }

    if(E_WS_SUCCESS != ws_start(config.server)) {
        /* listen or pthread error */
        DPRINTF(E_FATAL,L_MAIN|L_WS,"Error starting web server\n");
    }

    ws_registerhandler(config.server, "/",main_handler,main_auth,
                       0,1);

#ifndef WITHOUT_MDNS
    if(config.use_mdns) { /* register services */
        servername = conf_get_servername();

        memset(txtrecord,0,sizeof(txtrecord));
        txt_add(txtrecord,"txtvers=1");
        txt_add(txtrecord,"Database ID=%0X",util_djb_hash_str(servername));
        txt_add(txtrecord,"Machine ID=%0X",util_djb_hash_str(servername));
        txt_add(txtrecord,"Machine Name=%s",servername);
        txt_add(txtrecord,"mtd-version=" VERSION);
        txt_add(txtrecord,"iTSh Version=131073"); /* iTunes 6.0.4 */
        txt_add(txtrecord,"Version=196610");      /* iTunes 6.0.4 */
        tmp = conf_alloc_string("general","password",NULL);
        if(tmp && (strlen(tmp)==0)) tmp=NULL;

        txt_add(txtrecord,"Password=%s",tmp ? "true" : "false");
        if(tmp) free(tmp);

        srand((unsigned int)time(NULL));

        if(ffid) {
            txt_add(txtrecord,"ffid=%s",ffid);
        } else {
            txt_add(txtrecord,"ffid=%08x",rand());
        }

        DPRINTF(E_LOG,L_MAIN|L_REND,"Registering rendezvous names\n");
        iface = conf_alloc_string("general","interface","");

        rend_register(servername,"_http._tcp",ws_config.port,iface,txtrecord);

        plugin_rend_register(servername,ws_config.port,iface,txtrecord);

        free(servername);
        free(iface);
    }
#endif

    end_time=(int) time(NULL);

    err=db_get_song_count(&perr,&song_count);
    if(err != DB_E_SUCCESS) {
        DPRINTF(E_FATAL,L_MISC,"Error getting song count: %s\n",perr);
    }

    DPRINTF(E_LOG,L_MAIN,"Serving %d songs.  Startup complete in %d seconds\n",
            song_count,end_time-start_time);

    if(conf_get_int("general","rescan_interval",0) && (!reload) &&
       (!conf_get_int("scanning","skip_first",0)))
        config.reload = 1; /* force a reload on start */

    while(!config.stop) {
        if((conf_get_int("general","rescan_interval",0) &&
            (rescan_counter > conf_get_int("general","rescan_interval",0)))) {
            if((conf_get_int("general","always_scan",0)) ||
                (config_get_session_count())) {
                config.reload=1;
            } else {
                DPRINTF(E_DBG,L_MAIN|L_SCAN|L_DB,"Skipped bground scan... no users\n");
            }
            rescan_counter=0;
        }

        if(config.reload) {
            old_song_count = song_count;
            start_time=(int) time(NULL);

            DPRINTF(E_LOG,L_MAIN|L_DB|L_SCAN,"Rescanning database\n");

            if(conf_get_array("general","mp3_dir",&mp3_dir_array)) {
                if(config.full_reload) {
                    config.full_reload=0;
                    db_force_rescan(NULL);
                }

                if(scan_init(mp3_dir_array)) {
                    DPRINTF(E_LOG,L_MAIN|L_DB|L_SCAN,"Error rescanning... bad path?\n");
                }

                conf_dispose_array(mp3_dir_array);
            }
            config.reload=0;
            db_get_song_count(NULL,&song_count);
            DPRINTF(E_LOG,L_MAIN|L_DB|L_SCAN,"Scanned %d songs (was %d) in "
                    "%d seconds\n",song_count,old_song_count,
                    time(NULL)-start_time);
        }

        os_wait(MAIN_SLEEP_INTERVAL);
        rescan_counter += MAIN_SLEEP_INTERVAL;
    }

    DPRINTF(E_LOG,L_MAIN,"Stopping gracefully\n");

#ifndef WITHOUT_MDNS
    if(config.use_mdns) {
        DPRINTF(E_LOG,L_MAIN|L_REND,"Stopping rendezvous daemon\n");
        rend_stop();
    }
#endif

#ifdef UPNP
    upnp_deinit();
#endif


    /* Got to find a cleaner way to stop the web server.
     * Closing the fd of the socking accepting doesn't necessarily
     * cause the accept to fail on some libcs.
     *
    DPRINTF(E_LOG,L_MAIN|L_WS,"Stopping web server\n");
    ws_stop(config.server);
    */
    free(web_root);
    conf_close();

    DPRINTF(E_LOG,L_MAIN|L_DB,"Closing database\n");
    db_deinit();

    DPRINTF(E_LOG,L_MAIN,"Done!\n");

    os_deinit();
    io_deinit();
    mem_dump();
    return EXIT_SUCCESS;
}
Exemplo n.º 9
0
void		_main(void)
{
  uint32_t i;

  /**
   *
   *	Basic setup
   */

  /* enable the A20 line */
 
  /* disable the PIC (8259A chip) */
  pic_enable(false);

  /* disable the local APIC */
  //apic_enable(false);
  //apic_local_enable(false)

  /**
   *
   *	Protection
   */

  /* init and enable segmentation */
  //gdt_init();

  seg_init();
  //  INFINITE_LOOP();

  /* init and enable paging */
  //mmu_init();
  console_clear();
  mmu_init();

  /* init the system-call facilities */
  sc_init();

  /**
   *
   *	Interrupts
   */
  // INFINITE_LOOP();
  /* init and enable interrupts */
  idt_init();

  //INFINITE_LOOP();

  /* bind stage2 isrs */
  //isr_init();
  int_init();
  ex_init();

  //INFINITE_LOOP();

  /* configure the Local-APIC timer */
  //apic_local_timer_init(0x2ffffff);
  //apic_local_timer_init(0xffffff);

  /* configure the PIT timer */
  pit_init();

  printf("Local-APIC init...\n");
  /* Init the Local-APIC. */
  apic_local_init();
  apic_local_set_task_priority(0);
  printf("Local-APIC timer config...\n");
  apic_local_timer_config(0x2ffffff);
  printf("IO-APIC init...\n");
  apic_io_init();
  /* enable the local APIC */
  //apic_enable(true);
  apic_local_enable(true);

  printf("OK\n");

  /* configure the I/O APIC in charge of the PIT, keyboard, ... */
  //apic_io_init();
  /* STI(); */
/*   INFINITE_LOOP(); */

  /* clear the screen */
  console_clear();
  console_init();

  /* init the basic I/O services */
  stdio_init();
  //tty_init();
  //tty_activate(0);

 /*  sysenter(); */
  /*   vmx_supported(1); */
  test_malloc();

  /* init the serial number facility */
  serial_init();
  /*   printf("UID %d\n", serial_generate()); */
  /*   printf("UID %d\n", serial_generate()); */
  /*   printf("UID %d\n", serial_generate()); */

  /* init the task manager */
  thread_init();

  /* init the scheduler manager */
  sched_init();

  /* dump the memory */
  mem_dump();

  /* test the mmu. */
  //mmu_test();
  /* while (1) */
/*     ; */

  //video_init();

/*   msr = rdmsr(MSR_IA32_APIC_BASE); */
/*   printf("msr hi: %x\n", (uint32_t)(msr >> 32)); */
/*   printf("msr lo: %x\n", (uint32_t) msr); */
/*   msr = rdmsr(MSR_IA32_CR_PAT); */
/*   printf("msr hi: %x\n", (uint32_t)(msr >> 32)); */
/*   printf("msr lo: %x\n", (uint32_t) msr); */
/*   msr = rdmsr(MSR_IA32_PERF_GLOBAL_STATUS); */
/*   printf("msr hi: %x\n", (uint32_t)(msr >> 32)); */
/*   printf("msr lo: %x\n", (uint32_t) msr); */
/*   msr = rdmsr(MSR_IA32_PLATFORM_ID); */
/*   printf("msr hi: %x\n", (uint32_t)(msr >> 32)); */
/*   printf("msr lo: %x\n", (uint32_t) msr); */
/*   printf("Macrotest\n"); */
/*   MACROTEST(&msr); */
/*   printf("msr hi: %x\n", (uint32_t)(msr >> 32)); */
 /*  printf("apic-io ver: %x\n", apic_io_get_version()); */
/*   printf("apic-io id: %x\n", apic_io_get_id()); */

/*   printf("New GDT:\n"); */
/*   seg_init(); */
/*   printf("Old GDT:\n"); */
/*   for (i = 0; i < SEG_DESC_N; i++) */
/*     { */
/*       printf("%x - %x\n", gdt[i].high, gdt[i].low); */
/*     } */
  //INFINITE_LOOP();

  i = 1 << 5 | 1 << 9;
  printf("bsf(i) = %d\n", bsf(i));
  printf("bsr(i) = %d\n", bsr(i));
  i = 0;
  printf("bsr(i) = %d\n", bsr(i));

  printf("MSR_IA32_SYSENTER_CS high = %x\n", (uint32_t)(rdmsr(MSR_IA32_SYSENTER_CS) >> 32));
  printf("MSR_IA32_SYSENTER_CS low = %x\n", (uint32_t) rdmsr(MSR_IA32_SYSENTER_CS));

  printf("IA32_CR_PAT high = %x\n", (uint32_t)(rdmsr(MSR_IA32_CR_PAT) >> 32));
  printf("IA32_CR_PAT low = %x\n", (uint32_t) rdmsr(MSR_IA32_CR_PAT));

  /* jump to background task - code after this function is never reached */
  sched_launch();


  /*   test_multiline(); */
  /*   test_println(); */
  /*   test_interrupts(); */
  /*   test_itoa(); */
  /*   test_msr(); */
  //test_apic();
  //test_ide_dma();

  //printf("+ Bus 0\n", BG_BLACK | FG_RED | FG_INTENSITY);
  //pci_list(0, 2, 64);
  /*   printf("Bus 1\n", BG_BLACK | FG_RED | FG_INTENSITY); */
  /*   pci_list(1); */
  //test_ide_dma();
  //test_apic();

  //fprintf(0, "Protos v%d.%d - id:%x\n", 0, 12, 0xbe01);
  //fprintf(0, "\tid reg:\t%r\n", 0xbe01);
  //fprintf(0, "Ok!\n");
  // printf("Helo %% %d-%x-%r\n", 45, 0xabc78, 0xabc78);
  //printf("Helo %r\n", 0xbe01);

  //r = fprintf(stdout, "Almost %x years :)\n", 26);
  //fprintf(stdout, "size:%d\n", strlen("hello!"));

  //printf("fprintf result = %d\n", r);
  //printf(&((&stdio_filedes[0])->buffer[0]));

  //__asm__ ("rdtsc");


  // Halt the system
  while (1)
    HLT();
}
Exemplo n.º 10
0
/* Handle the TX ring */
static int dev_pos_oc3_handle_txring(struct pos_oc3_data *d)
{
   u_char pkt[POS_OC3_MAX_PKT_SIZE],*pkt_ptr;
   m_uint32_t clen,tot_len,norm_len;
   m_uint32_t tx_start,addr;
   struct tx_desc txd0,ctxd,*ptxd;
   int i,done = FALSE;

   if ((d->tx_start == 0) || (d->nio == NULL))
      return(FALSE);

   /* Copy the current txring descriptor */
   tx_start = d->tx_current;   
   ptxd = &txd0;
   txdesc_read(d,d->tx_current,ptxd);

   /* If we don't own the descriptor, we cannot transmit */
   if (!(txd0.tdes[0] & POS_OC3_TXDESC_OWN))
      return(FALSE);

#if DEBUG_TRANSMIT
   POS_LOG(d,"pos_oc3_handle_txring: 1st desc: tdes[0]=0x%x, tdes[1]=0x%x\n",
           ptxd->tdes[0],ptxd->tdes[1]);
#endif

   pkt_ptr = pkt;
   tot_len = 0;
   i = 0;

   do {
#if DEBUG_TRANSMIT
      POS_LOG(d,"pos_oc3_handle_txring: loop: tdes[0]=0x%x, tdes[1]=0x%x\n",
              ptxd->tdes[0],ptxd->tdes[1]);
#endif

      if (!(ptxd->tdes[0] & POS_OC3_TXDESC_OWN)) {
         POS_LOG(d,"pos_oc3_handle_txring: descriptor not owned!\n");
         return(FALSE);
      }

      clen = ptxd->tdes[0] & POS_OC3_TXDESC_LEN_MASK;

      /* Be sure that we have length not null */
      if (clen != 0) {
         addr = ptxd->tdes[1];

         norm_len = normalize_size(clen,4,0);
         physmem_copy_from_vm(d->vm,pkt_ptr,addr,norm_len);
         mem_bswap32(pkt_ptr,norm_len);
      }

      pkt_ptr += clen;
      tot_len += clen;

      /* Clear the OWN bit if this is not the first descriptor */
      if (i != 0)
         physmem_copy_u32_to_vm(d->vm,d->tx_current,0);

      /* Go to the next descriptor */
      txdesc_set_next(d,ptxd);

      /* Copy the next txring descriptor */
      if (ptxd->tdes[0] & POS_OC3_TXDESC_CONT) {
         txdesc_read(d,d->tx_current,&ctxd);
         ptxd = &ctxd;
         i++;
      } else
         done = TRUE;
   }while(!done);

   if (tot_len != 0) {
#if DEBUG_TRANSMIT
      POS_LOG(d,"sending packet of %u bytes (flags=0x%4.4x)\n",
              tot_len,txd0.tdes[0]);
      mem_dump(log_file,pkt,tot_len);
#endif   
      /* send it on wire */
      netio_send(d->nio,pkt,tot_len);
   }

   /* Clear the OWN flag of the first descriptor */
   txd0.tdes[0] &= ~POS_OC3_TXDESC_OWN;
   physmem_copy_u32_to_vm(d->vm,tx_start,txd0.tdes[0]);

   /* Interrupt on completion */
   pci_dev_trigger_irq(d->vm,d->pci_dev);
   return(TRUE);
}
Exemplo n.º 11
0
void test_mem() {
    int test_size = 2;
    void* list[amount/test_size];
    int list_cs[amount/test_size];
    int list_sizes[amount/test_size];

    int count = 0;
    void *p;

    int block_size = rand() % 100 + test_size;
    for(int i=0;i<rand()%20; i++) {
        p = mem_alloc(block_size);
        // printf("%d\n", p - mi.memory);
        char cs = cs_base;
        char value;
        for (int i=0; i<block_size; i++) {
            value = rand() % 255;
            *(char*)(p+i) = value;
            cs ^= value;
        }
        list_sizes[count] = block_size;
        list_cs[count] = cs;
        list[count++] = p;
        block_size = rand() % 100 + test_size;
    }
    mem_dump();
    // count --;
    printf("Allocated %d blocks of %d..%d b\n", count, test_size, test_size+100);


//CHECK CS
    bool cs_check = true;

    for(int i=0; i<count; i++) {
        char cs = cs_base;
        for (int j=0; j<list_sizes[i]; j++) {
            cs ^= *((char*)(list[i]+j));
        }
        cs_check = cs_check && (cs == list_cs[i]);
    }

    printf("Control sum check: %d\n", cs_check);

    printf("Realloc...\n");

//REALLOC
    int count2 = 0;
    // int test_size2 = 16;
    for(int i=0; i<count; i++) {
        block_size = rand() % 100 + test_size;
        printf("%d\n", block_size);
        void *p = mem_realloc(list[i], block_size);
        if (!p) {
            printf("NULL\n");
            count2++;
            continue;
        }
        char cs = cs_base;
        char value;
        for (int i=0; i<block_size; i++) {
            value = rand() % 255;
            *(char*)(p+i) = value;
            cs ^= value;
        }
        list_sizes[count2] = block_size;
        list_cs[count2] = cs;
        list[count2++] = p;
    }
    mem_dump();


//CHECK CS
    cs_check = true;

    for(int i=0; i<count2; i++) {
        char cs = cs_base;
        for (int j=0; j<list_sizes[i]; j++) {
            cs ^= *((char*)(list[i]+j));
        }
        cs_check = cs_check && (cs == list_cs[i]);
    }

    printf("Control sum check: %d\n\n", cs_check);

    printf("Free all\n");

    for(int i=0; i<count2; i++) mem_free(list[i]);
    mem_dump();
}