Skip to content
This repository has been archived by the owner on Jul 4, 2021. It is now read-only.

Commit

Permalink
Added linked list example.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Aug 7, 2008
1 parent 42e422c commit 8569ac0
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions .hgignore
Expand Up @@ -5,4 +5,5 @@ dataplugin_tests/hello/hello
dataplugin_tests/wchar_t/wchar32
dataplugin_tests/wchar_t/wchar16
dataplugin_tests/std_str/stdstr
dataplugin_tests/linkedlist/linkedlist

14 changes: 14 additions & 0 deletions dataplugin_tests/linked_list/Makefile
@@ -0,0 +1,14 @@
all: viewlinkedlist.so linkedlist

.PHONY: clean
clean:
rm -f linkedlist viewlinkedlist.so

linkedlist: linkedlist.cpp
g++ -o linkedlist -Wall -O0 -g linkedlist.cpp

viewlinkedlist.so: viewlinkedlist.cpp
g++ -o viewlinkedlist.so -shared -fPIC -Wall -O0 -g viewlinkedlist.cpp -I../../src/gdb

# end of Makefile ...

39 changes: 39 additions & 0 deletions dataplugin_tests/linked_list/linkedlist.cpp
@@ -0,0 +1,39 @@
#include <string.h>
#include "linkedlist.h"

static LinkedList *list = NULL;

static char *xstrdup(const char *str)
{
const size_t len = strlen(str);
char *retval = new char[len+1];
strcpy(retval, str);
return retval;
}

static void AddToList(const char *first, const char *last, int office)
{
LinkedList *item = new LinkedList;
item->first = xstrdup(first);
item->last = xstrdup(last);
item->office_number = office;
item->next = list;
list = item;
}

int main(void)
{
AddToList("Ryan", "Gordon", 4923);
AddToList("Greg", "Read", 227);
AddToList("Linus", "Torvalds", 732);
AddToList("Bill", "Gates", 666); // I kid! I kid!
AddToList("Richard", "Stallman", 1135);
AddToList("Andrew", "Tannenbaum", 9986);

// now leak all that memory. :)
return 0;
}

/* end of linkedlist.cpp ... */


16 changes: 16 additions & 0 deletions dataplugin_tests/linked_list/linkedlist.h
@@ -0,0 +1,16 @@
#ifndef _INCL_LINKEDLIST_H_
#define _INCL_LINKEDLIST_H_

class LinkedList
{
public:
const char *first;
const char *last;
int office_number;
LinkedList *next;
};

#endif

/* end of linkedlist.h ... */

38 changes: 38 additions & 0 deletions dataplugin_tests/linked_list/viewlinkedlist.cpp
@@ -0,0 +1,38 @@

#include "linkedlist.h"
#include "gdb-dataplugins.h"

static void view_linkedlist(const void *ptr, const GDB_dataplugin_funcs *funcs)
{
LinkedList item;

while (ptr)
{
if (funcs->readmem(ptr, &item, sizeof (item)) != 0)
return;

char *first = (char *) funcs->readstr(item.first, sizeof (char));
if (first)
{
char *last = (char *) funcs->readstr(item.last, sizeof (char));
if (last)
{
funcs->print("item (%p) { \"%s\", \"%s\", %d }%s\n",
ptr, first, last, item.office_number,
item.next ? "," : "");
funcs->freemem(last);
}
funcs->freemem(first);
}

ptr = item.next;
}
}

void GDB_DATAPLUGIN_ENTRY(const GDB_dataplugin_entry_funcs *funcs)
{
funcs->register_viewer("LinkedList", view_linkedlist);
}

/* end of viewlinkedlist.cpp ... */

0 comments on commit 8569ac0

Please sign in to comment.