Beispiel #1
0
Property *
DT__AddProperty(Node *node, const char *name, uint32_t length, void *value)
{
	Property *prop;

	DPRINTF("DT__AddProperty([Node '%s'], '%s', %d, 0x%X)\n", DT__GetName(node), name, length, value);

	if (freeProperties == NULL) {
		void *buf = malloc(kAllocSize);
		int i;

		DPRINTF("DT__AddProperty: Allocating more free properties\n");

		if (buf == 0) {
			return 0;
		}

		bzero(buf, kAllocSize);
		// Use the first property to record the allocated buffer
		// for later freeing.
		prop = (Property *)buf;
		prop->next = allocedProperties;
		allocedProperties = prop;
		prop->value = buf;
		prop++;

		for (i = 1; i < (kAllocSize / sizeof(Property)); i++) {
			prop->next = freeProperties;
			freeProperties = prop;
			prop++;
		}
	}

	prop = freeProperties;
	freeProperties = prop->next;

	prop->name = name;
	prop->length = length;
	prop->value = value;

	// Always add to end of list
	if (node->properties == 0) {
		node->properties = prop;
	} else {
		node->last_prop->next = prop;
	}

	node->last_prop = prop;
	prop->next = 0;

	DPRINTF("DT__AddProperty: done.\n");

	DTInfo.numProperties++;
	DTInfo.totalPropertySize += RoundToLong(length);

	return prop;
}
Beispiel #2
0
Node *
DT__FindNode(const char *path, bool createIfMissing)
{
    Node *node, *child;
    DTPropertyNameBuf nameBuf;
    char *bp;
    int i;

    DPRINTF("DT__FindNode('%s', %d)\n", path, createIfMissing);
    
    // Start at root
    node = rootNode;
    DPRINTF("root = 0x%x\n", rootNode);

    while (node) {
        // Skip leading slash
        while (*path == '/') path++;

        for (i=0, bp = nameBuf; ++i < kDTMaxEntryNameLength && *path && *path != '/'; bp++, path++) *bp = *path;
        *bp = '\0';

        if (nameBuf[0] == '\0') {
            // last path entry
            break;
        }
        DPRINTF("Node '%s'\n", nameBuf);

        for (child = node->children; child != 0; child = child->next) {
            DPRINTF("Child 0x%x\n", child);
            if (strcmp(DT__GetName(child), nameBuf) == 0) {
                break;
            }
        }
        if (child == 0 && createIfMissing) {
            DPRINTF("Creating node\n");
            char *str = malloc(strlen(nameBuf) + 1);
            // XXX this will leak
            strcpy(str, nameBuf);

            child = DT__AddChild(node, str);
        }
        node = child;
    }
    return node;
}